r语言 - 无法使用 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$国家。有谁知道我做错了什么?

这个呢:

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"))

最新更新