使用ggplot2 stat_sum时更改标题图例

时间:2014-09-26 10:00:42

标签: r ggplot2 legend

我想更改使用ggplot2构建的stat_sum图表的标题图例。 这里stat_sum不使用比例,而是使用总和。

数据:

data <- structure(list(replicate = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L), .Label = c("A", "B", "C"), class = "factor"), 
conc = c(0, 0, 0, 0.074, 0.074, 0.074, 0.22, 0.22, 0.22, 
0.66, 0.66, 0.66), time = c(21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L), Nsurv = c(18L, 19L, 19L, 19L, 
19L, 18L, 16L, 16L, 18L, 3L, 3L, 3L), Nrepro = c(161L, 102L, 
128L, 151L, 73L, 60L, 98L, 52L, 123L, 0L, 5L, 0L), conc2 = c(0, 
0, 0, 0.074, 0.074, 0.074, 0.22, 0.22, 0.22, 0.66, 0.66, 
0.66)), .Names = c("replicate", "conc", "time", "Nsurv", 
"Nrepro", "conc2"), row.names = c(43L, 44L, 45L, 88L, 89L, 90L, 
133L, 134L, 135L, 178L, 179L, 180L), class = "data.frame")

我使用:

创建ggplot对象
sp <- ggplot(data,aes(conc2, Nsurv)) +
stat_sum(aes(size = factor(..n..))) +
labs(x = "concentration",
y = "response")

我尝试了不同的功能来更改标题图例而没有结果:

sp + guides(fill=guide_legend(title = "points")) #no change
#or
sp + guides(color=guide_legend(title = "points")) #no change
sp + scale_fill_discrete(name = "points") #no change
#or
sp + guide_legend(title="points") #don't work
#or
sp + scale_colour_brewer(name = "points") #no change
#or
sp + scale_colour_hue("points") #no change

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您正尝试更改因子的大小比例的名称。因此,您需要将参数传递给scale_size_discrete

sp <- ggplot(data,aes(conc2, Nsurv)) +
  stat_sum(aes(size = factor(..n..))) +
  labs(x = "concentration",
       y = "response") +
  scale_size_discrete(name = "points")