Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does ggplot2 hide the legend

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is a detailed introduction to "ggplot2 how to hide the legend". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "ggplot2 how to hide the legend" can help you solve your doubts. The following is a detailed introduction to "ggplot2 how to hide the legend." Let's learn new knowledge together.

One uses the guide element guides to achieve:

Code:

library('ggplot2')library('reshape2')A = c("A","B","C","D","E")B = c(90,34,56,99,15)C = c(50,20,24,70,14)dat = data.frame(A,B,C)names(dat) = c("type","sample1","sample2")dat = melt(dat,variable.name="Sample",value.name = "Num")head(dat)p = ggplot(dat, aes(x = type,y = Num,fill = Sample))+ geom_bar(stat ="identity",width = 0.6,position = position_dodge(width=0.8))+ scale_fill_manual(values = c("red","blue"))+ labs(x = "",y = "", title = "test")+ geom_text(aes(label = dat$Num),position=position_dodge(width = 0.9),size = 5,vjust = -0.25)+ guides(fill = guide_legend(reverse = F))+ theme(plot.title = element_text(size = 25,face = "bold", vjust = 0.5, hjust = 0.5), legend.title = element_blank(), legend.text = element_text(size = 18, face = "bold"), legend.position = 'right', legend.key.size=unit(0.8,'cm'))print(p)

Hide the legend generated by fill with guides

p=p+guides(fill=F)p

Legend disappears.

If other geometric objects geom are involved, there may be other ways to generate legends, including aes(color, size, shape), etc., which can be generated through guides(color=F,size=F...) And so on to adjust the legend. This shows that when you hide a legend with guides, you can hide it locally, and you can operate on a specific legend.

Second, you can use theme(legend.position="none") to hide the global legend. for example

library('ggplot2')library('reshape2')A = c("A","B","C","D","E")B = c(90,34,56,99,15)C = c(50,20,24,70,14)dat = data.frame(A,B,C)names(dat) = c("type","sample1","sample2")dat = melt(dat,variable.name="Sample",value.name = "Num")head(dat)p = ggplot(dat, aes(x = type,y = Num,fill = Sample))+ geom_bar(stat ="identity",width = 0.6,position = position_dodge(width=0.8))+ scale_fill_manual(values = c("red","blue"))+ labs(x = "",y = "", title = "test")+ geom_text(aes(label = dat$Num),position=position_dodge(width = 0.9),size = 5,vjust = -0.25)+ guides(fill = guide_legend(reverse = F))+ theme(plot.title = element_text(size = 25,face = "bold", vjust = 0.5, hjust = 0.5), legend.position = "none")print(p)

This method hides all legends at the same time, and can be selected if multiple legend manipulations occur.

Read here, this article "ggplot2 how to hide legend" article has been introduced, want to master the knowledge points of this article also need to practice to understand, if you want to know more related content articles, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report