我想更改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。有人知道我在做什么错吗?
答案 0 :(得分:0)
那呢:
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))
答案 1 :(得分:0)
这有效吗? (如果附加一些数据,则更容易回答您的问题。)
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"))