gpt4 book ai didi

在图例 ggplot2 中反射(reflect)线型

转载 作者:行者123 更新时间:2023-12-04 08:16:08 25 4
gpt4 key购买 nike

在我下面的图表中,我希望图例反射(reflect)实际的线型,即“距离”的蓝线也应该在图例中用虚线表示。我如何实现这一目标?
在我的代码下面。谢谢!

data <- structure(list(Year = c(1980, 1981, 1982, 1983, 1984, 1985, 1986, 
1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016), Region = c(428,
424, 424, 424, 424, 423, 423, 423, 423, 423, 423, 423, 423, 423,
423, 427, 427, 429, 428, 428, 427, 427, 426, 429, 431, 429, 429,
430, 430, 431, 433, 435, 434, 436, 434, 434, 432), Distance = c(45.3851417635712,
45.1015566515805, 45.0949633134224, 44.9843501596268, 44.472001374814,
44.4651674044446, 44.4572188553101, 44.2469544378131, 44.2469544378131,
44.2469544378131, 44.2132321845356, 43.8753401319457, 42.6315852081922,
42.0628336732444, 42.0628336732444, 41.8727936463309, 41.8727936463309,
41.7635411376391, 41.7635411376391, 41.7635411376391, 41.7635411376391,
41.7635411376391, 41.7635411376391, 41.6326581679158, 41.2193523001234,
41.1576215657083, 41.1576215657083, 41.0389941582308, 40.8914901030284,
40.8257256986156, 40.8257256986156, 40.670233047203, 40.670233047203,
40.512318467256, 40.512318467256, 40.512318467256, 40.3362918835934
)), row.names = c(NA, -37L), class = c("tbl_df", "tbl", "data.frame"
))


library(ggplot2)
p <- ggplot(data, aes(x = Year))
p <- p + geom_line(aes(y = data$Region, color = "Region"), size=1.1)
p <- p + scale_x_continuous(breaks = seq(1980, 2015, by = 5))

# first axis
p <- p + geom_line(aes(y = data$Distance*10, color = "Distance"), size=1.1, linetype = "dashed")

# now adding the secondary axis
p <- p + scale_y_continuous(sec.axis = sec_axis(~./10, name = "Distance"))
p <- p + labs(y = "Region")



# modifying colours and theme options
p <- p + theme(legend.position = c(0.8, 0.9))
p <- p + scale_color_manual(values=c('#67a9cf','#ef8a62')) + theme(text=element_text(size=12, family="serif"))
p <- p + theme(legend.position="bottom") + theme(legend.title=element_blank(), axis.text=element_text(size=11), axis.title=element_text(size=11), legend.text=element_text(size=10),panel.grid.minor = element_blank())
p <- p + annotate("rect", xmin=1988, xmax=1997, ymin=-Inf, ymax=Inf, alpha=0.1, fill="#999999")
p <- p + annotate("text", x = 1992.25, y = 452, label = "xxx", family="serif")+ annotate("text", x = 1992, y = 450, label = "yyy", family="serif")
p <- p + theme(text=element_text(size=12, family="serif"))
p <- p+ theme(panel.background = element_rect(fill = NA),
panel.grid.major = element_line(colour = "gray95"),
panel.border = element_rect(colour = "gray20", fill=NA, size=0.5))
p
enter image description here

最佳答案

您可以包含 linetypeaes并分配 linetype 的类型使用 scale_linetype_manual .

library(ggplot2)

ggplot(data, aes(x = Year)) +
geom_line(aes(y = Region, linetype = "Region", color = "Region"), size=1.1) +
scale_x_continuous(breaks = seq(1980, 2015, by = 5)) +
geom_line(aes(y = Distance*10, linetype = "Distance", color = "Distance"), size=1.1) +
scale_y_continuous(sec.axis = sec_axis(~./10, name = "Distance")) +
labs(y = "Region") +
scale_linetype_manual("colour",values=c("Distance"= "dotted", "Region" = "solid")) +
theme(legend.title=element_blank(),
legend.position="bottom")
enter image description here

关于在图例 ggplot2 中反射(reflect)线型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65696572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com