ggplot 图例中的虚线
dashed line in ggplot legend
我想设置图例的线型。
我的数据是这样的:
VisitMonth VisitYear CaixaForum MNAC FirstDay
1: 01 2007 NA 7125 2007-01-01
2: 02 2007 NA 5345 2007-02-01
3: 03 2007 NA 4996 2007-03-01
4: 04 2007 NA 5476 2007-04-01
5: 05 2007 NA 6160 2007-05-01
---
98: 02 2015 17903 2360 2015-02-01
99: 03 2015 30400 2930 2015-03-01
100: 04 2015 25422 3088 2015-04-01
101: 05 2015 10787 2130 2015-05-01
102: 06 2015 3679 2047 2015-06-01
我想绘制CaixaForum 和MNAC 专栏的时间序列。我有以下代码:
ggplot(data = MUSEUMS, aes(x = FirstDay, y = MNAC)) +
geom_line(size=0.75, aes(x = FirstDay, y = MNAC, colour = "MNAC")) +
geom_line(size=0.75, aes(y = CaixaForum, colour = "CaixaForum"), linetype = "dashed") +
labs(title = "", x = "", y = "Monthly Visitors") + theme_bw() +
theme(legend.title = element_text(size=16, face="bold"), legend.direction = "horizontal",
legend.position=c(0.5, 1.05), text = element_text(size=20)) +
scale_colour_manual(name="Legend",values=c(MNAC="black", CaixaForum="black"))
如您所见,您无法区分图例中的两种线型:
我该如何解决?
我在 Whosebug 中编写了其他答案,但它们没有用。
我创建了一些随机数据来演示解决方案...
library(reshape2)
df <- data.frame(a=rnorm(10), b=rnorm(10), x=1:10, other_data=rnorm(10))
mdf <- melt(df, id.vars='x', measure.vars=c('a','b'))
ggplot(mdf, aes(x, value, linetype=variable)) + geom_line()
您可以在两个点图层中切换为使用 linetype
而不是 color
,因为您实际上并未对图形中的任何内容使用 color
。
看起来像这样:
ggplot(data = MUSEUMS, aes(x = FirstDay, y = MNAC)) +
geom_line(size=0.75, aes(x = FirstDay, y = MNAC, linetype = "MNAC")) +
geom_line(size=0.75, aes(y = CaixaForum, linetype = "CaixaForum")) +
labs(title = "", x = "", y = "Monthly Visitors") + theme_bw() +
theme(legend.title = element_text(size=16, face="bold"), legend.direction = "horizontal",
legend.position=c(0.5, 1.05), text = element_text(size=20)) +
scale_linetype_manual(name="Legend",values=c(MNAC="solid", CaixaForum="dashed"))
如果出于某种原因你真的想使用你现在使用的方法,你可以通过在 guide_legend
中的 override.aes
获得你想要的行,方法是将以下行添加到你的图形中:
guides(color = guide_legend(override.aes = list(linetype = c("solid", "dashed"))))
我遇到了同样的问题。我认为这与图例线太小而无法显示破折号有关。添加:
theme(legend.key.size=unit(3,"lines"))
帮我修好了。
我想设置图例的线型。
我的数据是这样的:
VisitMonth VisitYear CaixaForum MNAC FirstDay
1: 01 2007 NA 7125 2007-01-01
2: 02 2007 NA 5345 2007-02-01
3: 03 2007 NA 4996 2007-03-01
4: 04 2007 NA 5476 2007-04-01
5: 05 2007 NA 6160 2007-05-01
---
98: 02 2015 17903 2360 2015-02-01
99: 03 2015 30400 2930 2015-03-01
100: 04 2015 25422 3088 2015-04-01
101: 05 2015 10787 2130 2015-05-01
102: 06 2015 3679 2047 2015-06-01
我想绘制CaixaForum 和MNAC 专栏的时间序列。我有以下代码:
ggplot(data = MUSEUMS, aes(x = FirstDay, y = MNAC)) +
geom_line(size=0.75, aes(x = FirstDay, y = MNAC, colour = "MNAC")) +
geom_line(size=0.75, aes(y = CaixaForum, colour = "CaixaForum"), linetype = "dashed") +
labs(title = "", x = "", y = "Monthly Visitors") + theme_bw() +
theme(legend.title = element_text(size=16, face="bold"), legend.direction = "horizontal",
legend.position=c(0.5, 1.05), text = element_text(size=20)) +
scale_colour_manual(name="Legend",values=c(MNAC="black", CaixaForum="black"))
如您所见,您无法区分图例中的两种线型:
我该如何解决?
我在 Whosebug 中编写了其他答案,但它们没有用。
我创建了一些随机数据来演示解决方案...
library(reshape2)
df <- data.frame(a=rnorm(10), b=rnorm(10), x=1:10, other_data=rnorm(10))
mdf <- melt(df, id.vars='x', measure.vars=c('a','b'))
ggplot(mdf, aes(x, value, linetype=variable)) + geom_line()
您可以在两个点图层中切换为使用 linetype
而不是 color
,因为您实际上并未对图形中的任何内容使用 color
。
看起来像这样:
ggplot(data = MUSEUMS, aes(x = FirstDay, y = MNAC)) +
geom_line(size=0.75, aes(x = FirstDay, y = MNAC, linetype = "MNAC")) +
geom_line(size=0.75, aes(y = CaixaForum, linetype = "CaixaForum")) +
labs(title = "", x = "", y = "Monthly Visitors") + theme_bw() +
theme(legend.title = element_text(size=16, face="bold"), legend.direction = "horizontal",
legend.position=c(0.5, 1.05), text = element_text(size=20)) +
scale_linetype_manual(name="Legend",values=c(MNAC="solid", CaixaForum="dashed"))
如果出于某种原因你真的想使用你现在使用的方法,你可以通过在 guide_legend
中的 override.aes
获得你想要的行,方法是将以下行添加到你的图形中:
guides(color = guide_legend(override.aes = list(linetype = c("solid", "dashed"))))
我遇到了同样的问题。我认为这与图例线太小而无法显示破折号有关。添加:
theme(legend.key.size=unit(3,"lines"))
帮我修好了。