简体   繁体   English

使用 ggplot 的 stat_summary 更改主题和图例元素

[英]Change theme and legend elements with stat_summary of ggplot

I have the following code:我有以下代码:

drawCombinedSeries <- function(data, xData, yData, dataGroup, title, fileName, outputPath) {
  
  plt <- ggplot(data, aes(x = xData, y = yData, fill = dataGroup)) +
    stat_summary(geom = "line", size=1, fun = mean, aes(color=dataGroup, group = dataGroup)) +
    stat_summary(geom = "ribbon", fun.data = mean_se, alpha = 0.3, aes(fill = dataGroup)) +
    labs(title = title, x="Month", y="") +
    theme(legend.position="bottom", axis.title.x = element_blank(), text = element_text(size=12, colour="black")) +
    scale_x_discrete(name = "Month", limits=c(1:12), expand = c(0,0)) 
  
  setwd(outputPath)
  ggsave(filename=fileName, width=10, height = 2.5)
  return(plt)
}

This function returns this plot:此函数返回此图:

活动图

What I desire is to change the colors of the lines to a grayscale, and rename the legend.我想要的是将线条的颜色更改为灰度,并重命名图例。

I tried to use theme_bw() , but it doesn't work because I already have my theme() .我尝试使用theme_bw() ,但它不起作用,因为我已经有了我的theme() Regarding the legend, when I try to create my own legend, it only creates one for the last stat_summary , which is the ribbon, thus having two legends.关于图例,当我尝试创建自己的图例时,它只为最后一个stat_summary创建一个图例,即功能区,因此有两个图例。 I have looked for a way to deal with two stat_summary functions, because I think that's the issue, but I haven't found anything enlightening.我一直在寻找一种方法来处理两个stat_summary函数,因为我认为这就是问题所在,但我没有发现任何有启发性的东西。

I solved the problem of the legend name using the labs function changing the color and fill parameters.我使用labs函数更改colorfill参数解决了图例名称的问题。 In the plot, I have the color (which is the line) and the fill (which is the ribbon), thus having to change both to rename the legend.在图中,我有颜色(即线条)和填充物(即色带),因此必须更改两者以重命名图例。

I attach the working code.我附上工作代码。

drawCombinedSeries <- function(data, xData, yData, dataGroup, title, fileName, outputPath) {
  
  plt <- ggplot(data, aes(x = xData, y = yData, fill = dataGroup)) +
    stat_summary(geom = "line", size=.6, fun = mean, aes(color=dataGroup, group = dataGroup)) +
    stat_summary(geom = "ribbon", fun.data = mean_se, alpha = 0.25, aes(fill = dataGroup)) +
    labs(title = title, x="Month", y="", fill = "Status", color = "Status") +
    theme(legend.position="bottom", axis.title.x = element_blank(), text = element_text(size=12, colour="black"), panel.background = element_blank(), panel.grid = element_line(colour = "gray")) +
    scale_x_discrete(name = "Month", limits=c(1:12), expand = c(0,0)) +
    scale_fill_manual(values = c('Alive' = "#666666",'Zombie' = "#777777", 'Dead' = "#ffffff")) +
    scale_color_manual(values = c('Alive' = "#666666",'Zombie' = "#777777", 'Dead' = "#ffffff"))
  
  setwd(outputPath)
  ggsave(filename=fileName, width=10, height = 2.5)
  return(plt)
} 

I have used the scale_color_manual and scale_fill_manual to have a better visualization on the overlapping of the ribbons.我使用scale_color_manualscale_fill_manual对色带的重叠进行了更好的可视化。 To rename the legend, I have changed the fill and color field of labs() function.为了重命名图例,我更改了labs()函数的fillcolor字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM