如何更改“主题”中的图例标签ggplot2中的参数? [英] How to change legend label in "theme" argument in ggplot2?

查看:242
本文介绍了如何更改“主题”中的图例标签ggplot2中的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ggplot2绘制错误条形图。对于图例,我想要这样:


  1. 里面的情节。我知道功能主题(legend.position)可以做到这一点。


  2. 在带有函数表达式的图例标签中使用乳胶符号。


我已经阅读了构建图例的几个引用,但仍然没有找到我想要的东西。我可以执行1或2,但不能在我的情节中执行这两个操作。



以下是有问题情节的R代码。由于我的声望不足以发布图像,请将代码复制到您的R中以查看情节。我只想保持标签包含\ beta_A和\ beta_B,但不包含组1和组2的标签。



任何想法?谢谢!

  library(ggplot2); library(scales)#for alpha 
varx< - rep(c(group1,group2),each = 3)
vary< - rep(c(A,B ,C),2)
poi < - 样本(20:30,6)
上限< - 样本(40:50,6)
低< - 样本(1:10,6)

dat < - data.frame(varx,vary,poi,upper,lower)
dat
#varx vary poi upper lower
#1 group1 A 29 42 10
#2 group1 B 21 48 9
#3 group1 C 26 47 8
#4 group2 A 30 44 4
#5 group2 B 27 49 6
#6 group2 C 24 43 7

pp < - ggplot(dat,aes(color = varx,y = poi,x = vary))

限制
pp + geom_point(aes(shape = varx),position = position_dodge(0.3),size = 2)+
## dodge使行不会相互叠加
geom_errorbar(limits,size = 1,width = 0.15,position = position_dodge(0.3))+
theme_bw()+ ##摆脱灰色背景
geom_hline (yintercept = 0,linetype =dotdash)+
coord_cartesian(ylim = c(1,60))+
scale_shape_manual(values = c(17,19,18))+
scale_colour_hue(labels = c(expression(beta [A]),expression(beta [N])))+
theme(plot.margin = unit(rep(1.5,4),mm),
legend.justification = c(1,0),
legend.position = c(1,0.5),
legend.key = element_blank(),##摆脱图例框
legend.title = element_blank(),
legend.text = element_text(size = 10,face =bold),
legend.background = element_rect(fill = alpha(0.0001)))+
labs(x = NULL,y = NULL)


解决方案

要移除图例,您可以简单地将指南(shape = FALSE)添加到您的图中。在指南中,您可以决定哪些美学应该显示图例。要合并图例,使标签保持不变, ggplot 将完成其余的操作。

  ##将这个标签用于美学传奇
labels = c(表达式(beta [A]),表达式(beta [N]))

pp + geom_point(aes(形状= varx),position = position_dodge(0.3),size = 2)+
## dodge使行不相互叠加
geom_errorbar(限制,大小= 1,宽度= 0.15,位置= position_dodge(0.3))+
theme_bw()+ ##摆脱灰色背景
geom_hline(yintercept = 0,linetype =dotdash)+
coord_cartesian(ylim = c(1 ,60))+
scale_shape_manual(values = c(17,19,18),labels = labels)+
scale_colour_hue(labels = labels)+
theme(plot.margin = unit( (1.5,4),mm),
legend.justification = c(1,0),
legend.position = c(1,0.5),
legend.key = element_blank(),##摆脱图例框
legend.title = element_blank(),
legend.text = element_text(size = 10,face =bold),
legend.background = element_rect(fill = alpha(0.0001)))+
labs(x = NULL,y = NULL)#+ shape = FALSE)#如果想删除


I am using ggplot2 ro draw error bar plot. For the legend, I want it like this:

  1. inside the plot. I know function theme(legend.position) can do this.

  2. use latex symbol in the legend labels with function expression.

I have read several references of constructing the legend, but still have not found what I want exactly. I can perform either 1 or 2, but can not do both in my plot.

Following is R code of the plot with the problem. Since my reputation is not enough to post an image, please copy the code into your R to see the plot. I only want to keep the label contains \beta_A and \beta_B, but not the label contains group 1 and group 2.

Any ideas? Thank you!

library(ggplot2); library(scales) #for alpha
varx <- rep(c("group1", "group2"), each = 3)
vary <- rep(c("A", "B", "C"), 2)
poi <- sample(20:30, 6)
upper <- sample(40:50, 6)
lower <- sample(1:10, 6)

dat <- data.frame(varx, vary, poi, upper, lower)
dat
  #      varx vary poi upper lower
  # 1 group1    A  29    42    10
  # 2 group1    B  21    48     9
  # 3 group1    C  26    47     8
  # 4 group2    A  30    44     4
  # 5 group2    B  27    49     6
  # 6 group2    C  24    43     7

pp <- ggplot(dat, aes(colour = varx, y = poi, x = vary)) 

limits <- aes(ymax = upper, ymin = lower)

pp  + geom_point(aes(shape=varx), position = position_dodge(0.3), size = 2) + 
  ## dodge make the lines not stack upon each other
  geom_errorbar(limits, size = 1, width = 0.15, position = position_dodge(0.3)) + 
  theme_bw() + ## Get rid of the grey background
  geom_hline(yintercept = 0, linetype = "dotdash") +
  coord_cartesian(ylim = c(1, 60)) + 
  scale_shape_manual(values = c(17, 19, 18)) + 
  scale_colour_hue(labels =  c(expression(beta[A]), expression(beta[N]))) + 
  theme(plot.margin = unit(rep(1.5, 4),"mm"),
        legend.justification= c(1, 0), 
        legend.position = c(1, 0.5),
        legend.key = element_blank(),## Get rid of the legend box 
        legend.title = element_blank(),
        legend.text = element_text(size = 10, face = "bold"),
        legend.background = element_rect(fill=alpha(0.0001))) +
  labs(x = NULL, y = NULL)

解决方案

To remove a legend you can simply add guides(shape=FALSE) to your plot. In guides you can decide which aesthetics should display legends. To combine legends, make the labels the same and ggplot will do the rest.

## Use this label for both aesthetic legends
labels =  c(expression(beta[A]), expression(beta[N]))

pp  + geom_point(aes(shape=varx), position = position_dodge(0.3), size = 2) + 
  ## dodge make the lines not stack upon each other
  geom_errorbar(limits, size = 1, width = 0.15, position = position_dodge(0.3)) + 
  theme_bw() + ## Get rid of the grey background
  geom_hline(yintercept = 0, linetype = "dotdash") +
  coord_cartesian(ylim = c(1, 60)) + 
  scale_shape_manual(values = c(17, 19, 18), labels=labels) +
  scale_colour_hue(labels = labels) + 
  theme(plot.margin = unit(rep(1.5, 4),"mm"),
        legend.justification= c(1, 0), 
        legend.position = c(1, 0.5),
        legend.key = element_blank(),## Get rid of the legend box 
        legend.title = element_blank(),
        legend.text = element_text(size = 10, face = "bold"),
        legend.background = element_rect(fill=alpha(0.0001))) +
  labs(x = NULL, y = NULL) # + guides(shape=FALSE) # if wanting to remove

这篇关于如何更改“主题”中的图例标签ggplot2中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆