繁体   English   中英

无法使用ggplot更改图例标题和标签

[英]Not able to change legend title and labels using ggplot

我想更改ggplot中的标题和标签。 我现在有以下代码:

ggplot() +
geom_line(data = VW_activations, aes(x = VW_activations$Month, y = 
VW_activations$Activations, color = VW_activations$Country), size = 1.2)+
theme_light(base_size = 11, base_family = "")+
xlab("Date")+
ylab("Number of contract activations")+
scale_fill_discrete(name="Country", labels=c("AT", "BE", "CH", "DE", "ES", 
"FI", "FR", "IT", "LU", "NL", "NO", "PT", "UK"))

但是,这不起作用,图例标题只是VW_activations $ Country。 有人知道我在做什么错吗?

那这个呢:

ggplot() +
  geom_line(data = VW_activations, aes(x = Month, y = Activations, color = Country), size = 1.2)+
  theme_light(base_size = 11, base_family = "")+
  xlab("Date")+
  ylab("Number of contract activations")+
  scale_color_manual(name="Country", labels=c("AT", "BE"), values =c("red","green"))

要没有手动设置颜色数量,可以使用软件包RColorBrewer

library(RColorBrewer)
ggplot() +
  geom_line(data = VW_activations, aes(x = Month, y = Activations, color = Country), size = 1.2)+
  theme_light(base_size = 11, base_family = "")+
  xlab("Date")+
  ylab("Number of contract activations")+
  scale_color_manual(name="Country", labels=c("AT", "BE"),
  # here we define a scale of color between red and blue f.e., and generate
  # n colors n == numbers of country in this case
  values =colorRampPalette(c("red", "blue"))(length(unique(VW_activations$Country)))
   )

或者:

... values = brewer.pal(n = length(unique(VW_activations$Country)), name = "Set2")...

没有渐变“褪色”的颜色。 附带数据:

VW_activations <-data.frame(Country = c("K","J","K","J"),
                            Month = c(1,1,2,2),
                            Activations = c(11,13,55,66))

这样行吗? (如果附加一些数据,则更容易回答您的问题)。

ggplot() +
    geom_line(data = VW_activations, aes(x = VW_activations$Month, 
                                         y = VW_activations$Activations, 
                                         color = VW_activations$Country), 
              size = 1.2) +
    theme_light(base_size = 11, base_family = "") +
    labs(
        x = "Date",
        y = "Number of contract activations",
        fill = "Country"
        ) +
    scale_fill_discrete(labels = c("AT", "BE", "CH", "DE", "ES", "FI", "FR", 
                                   "IT", "LU", "NL", "NO", "PT", "UK"))

暂无
暂无

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

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