从ggplot图例更改符号大小和删除摘要

时间:2016-12-18 19:13:26

标签: r ggplot2

我是在R中绘制图形的新手,可以帮助找出ggplot图例问题。我编写了这段代码,首先包括我的缩写数据集:

data <- structure(list(date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
                                      2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                      2L), .Label = c("11/11/2016", "12/2/2016"), class = "factor"), 
                   site = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
                                      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                      2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
                   ), .Label = c("mk", "tf"), class = "factor"), factor = c(2L, 
                                                                            2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 10L, 10L, 10L, 10L, 10L, 
                                                                            10L, 10L, 10L, 10L, 10L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                            1L, 1L, 1L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
                                                                            541L), individual = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
                                                                                                  1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 1L, 2L, 3L, 
                                                                                                  4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
                                                                                                  8L, 9L, 10L), temp = c(-19.85, -19.94, -20.77, -21.3, -21.71, 
                                                                                                                         -21.88, -22.03, -22.74, -22.86, -16.63, -19.01, -19.67, -20.47, 
                                                                                                                         -21.14, -21.23, -23.01, -24.43, -24.61, -24.76, -19.09, -18.88, 
                                                                                                                         -19.02, -19.22, -19.32, -19.32, -19.55, -19.68, -20.23, -20.32, 
                                                                                                                         -21.37, -15.9, -18.87, -19.02, -19.16, -19.44, -19.62, -22.38, 
                                                                                                                         -24.37, -24.92, -26.9)), .Names = c("date", "site", "factor", 
                                                                                                                                                             "individual", "temp"), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                        -40L))

library(ggplot2)
library(plotrix) #loads standard error function

p <- ggplot(data, aes(x = factor(date), fill = factor(site), y = temp)) +
  geom_dotplot(binaxis = 'y', stackdir = 'center', method = 'histodot', binwidth = 0.3, position=position_dodge(0.5)) +
  labs(title="data plot",x = "sampling date", y = "response temp (°C)") +
  theme_classic() +
  theme(axis.title.y = element_text(vjust=1)) +
  theme(plot.title = element_text(size = rel(1.5))) +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_fill_discrete(name  ="",
                      breaks=c("mk", "tf"),
                      labels=c("site 1", "site 2")) +
  theme(legend.position="top") 
  #guides(colour = guide_legend(override.aes = list(size=xx))) # this last line doesn't seem to change the size of the legend dots.. 

p 

#mean dot with standard error lines:
m.sterr <- function(x) {
  m <- mean(x)
  ymin <- m-std.error(x)
  ymax <- m+std.error(x)
  return(c(y=m,ymin=ymin,ymax=ymax))
}

p + 
  stat_summary(fun.data=m.sterr, color="black", position=position_dodge(0.5), size = .25)

这产生了以下点图:

dot plot

我遇到的挑战是我的黑色stat_summary符号模糊了填充美学图例键。当我将stat_summary size参数更改为一个非常小的值时,它会使底层填充美学键中的颜色可见,但是绘图本身中的摘要对象变得太小。我认为这可以通过

来解决

(1)仅增加填充美学图例符号的大小,以便可以轻松识别该键,或

(2)更清洁,只是使黑点和条形摘要符号从图例中消失。

我还没弄明白如何做其中任何一个。对于(1),我尝试添加guides(colour = guide_legend(override.aes = list(size=xx)))并调整size参数,但这没有任何效果。

是否有任何建议可以完成上述修复(1)或(2),或者更好地解决我的传奇困境?

这是我的第一篇文章,所以我还在学习我需要包含的内容。如果需要更多细节,请告诉我。并感谢任何想法。

约书亚

0 个答案:

没有答案