控制ggplot图例中的线条颜色和线条类型 [英] Controlling line color and line type in ggplot legend
问题描述
背景 。在某些方面,例如某些癌症的死亡率,在十个前西方国家和六个前东方国家之间存在持续的差异。各组之间的州也有所不同。为了显示各州之间的差异,绘制数据可以有一定的意义,例如年龄 - 每年从各州标准化乳腺癌死亡率。有16行的情节并不总是一个好的选择,我不想就此进行讨论。有时这些权力可以说是它的原因。
问题
<区分情节中的16行可能很困难。为此,我通常使用来自
RColorBrewer
包(前缀格子
包,从1998年至2010年的州的年龄标准化乳腺癌死亡率情节可能如下所示:
问题
我想用 ggplot
做类似的情节,但我没有' t想出了如何在图例中结合颜色和线条类型。到目前为止,我已经得到了这些:
如果可以在 ggplot
传说中组合颜色和线条类型,这样做是为了创建数据框和图:
<$ (c)(8L,9L,11L,12L,4L,2L,
6L,13L,3L,5L,7L,10L,14L,15L, 1L,16L,8L,9L,11L,12L,
4L,2L,6L,13L,3L,5L,7L,10L,14L,15L,1L,16L,8L, 12L,4L,2L,6L,13L,3L,5L,7L,10L,14L,15L,1L,16L,
8L,9L,11L,12L,4L,2L,6L,13L,3L,5L, 7L,10L,14L,15L,
1L,16L,8L,9L,11L,12L,4L,2L,6L,13L,3L,5L,7L,10L,
14L,15L, 16L,8L,9L,11L,12L,4L,2L,6L,13L,3L,5L,
7L,10L,14L,15L,1L,16L,8L,9L,11L,12L,4L, 6L,13L,
3L,5L,7L,10L,14L,15L,1L,16L,8L,9L,11L,12L,4L,2L,
6L,13L,3L,5 L,7L,10L,14L,15L,1L,16L,8L,9L,11L,12L,
4L,2L,6L,13L,3L,5L,7L,10L,14L,15L,1L, 8L,9L,
11L,12L,4L,2L,6L,13L,3L,5L,7L,10L,14L,15L,1L,16L,
8L,9L,11L,12L,4L, 2L,6L,13L,3L,5L,7L,10L,14L,15L,
1L,16L,8L,9L,11L,12L,4L,2L,6L,13L,3L,5L,7L,10L,
14L,15L,1L,16L,8L,9L,11L,12L,4L,2L,6L,13L,3L,5L,
7L,10L,14L,15L,1L,16L) =factor,.Label = c(SH,
HH,NI,HB,NW,HE,RP,BW,BY, SL,BE,BB,
MV,SN,ST,TH)),BCmort = c(16.5,16.6,15,14.4,13.5,
17.1,15.8,16.3,18.3,16.8,17,18.1,13.1,15.1,18.8,13.1,
16.4,16.1,15.8,12.8,16.3,19.2,16.8,13,17.9,17,19.4,
19.4,13.1,13.8,18.1,13.8,15.9,17.3,17.5,13.7,17.4,17.5,
16.7,15.5,18.1,18,20.1,19.1,11.8,14.6,18.2,13.4, 16.8,
17.5,15.6,14.1,13.9,18.2,17.1,15.2,18.1,16.6,19.3,18.6,
13.1,14.6,19.6,12.4,16.6,17.8,17.5, 14.3%,20.5%,19.2%,19%,
12.6,19.5,17.8,19.2,21,14.4,13.4,19.8,14,17.5,18.9,
16.4,14.7,17.7,20.1,18.5,14.5, 19.1,19.2,20.1,19.7,14.2,
16.2,17.9,12.6,18,18.7,17.7,16.5,16.6,20.3,18.1,15.2,
19,20,19.8,21.3,13.8, 14.8,20.4,14.8,18.2,18.7,16.9,
16.2,20.2,20.4,18.5,14,20.2,18.7,20.3,17.7,14.4,14.5,
21.7,13.7,18.3,19.7, 17.8,16.5,20.2,21.7,18.8,16.7,20.4,
20,19.6,22.9,15.2,14.9,21.7,14.6,18.3,19.7,17.16.7,
22.9,16.2,19.6, 15.9,20.3,19.9,18.9,21.8,14.9,18,21.4,
16.1,19.6,19.2,19.1,16.7,20,18.2,20.5,15.5,20.5,21.1,
21.3,23.8, 15.8,15.3,21.3,15.7,19.6,20.3,19.2,17.4,18.1,
23.1,20.6,16.2,21.5,20.3,21.4,20.8,16.1,15.8,22.1,14.5,
20, 20.2,19,18.7,23.1,21.8,19.4,17.4,20.9,20.5,20.4,
23.2,16.3,17.6,23.1,16.5),年= c(2010,2010,2010,2010,
2010,2010,2010,2010,2010,2010,2010,2010,2010,2010,2 010,2009年,2009年,2009年,2009年,2009年,2009年,2009年,2009年,2009年,2009年,2009年,2009年,2009年,2009年,2009年,2009年,2008年,2008年,2008年, 2008年,2008年,2008年,2008年,2008年,2008年,2008年,2008年,2008年,2008年,2008年,2008年,2008年,2007年2007年2007年2007年2007年2007年2007年2007年2007年, 2007年,2007年,2007年,2007年,2007年,2007年,2007年,2007年,2006年,2006年,2006年,2006年,2006年,2006年,2006年,2006年,2006年,2006年,2006年,2006年,2006年, 2005年,2005年,2005年,2005年,2005年,2005年,2005年,2005年,2005年,2005年,
2005年,2005年,2005年,2005年,2004年,2004年, 2004年,2004年,2004年,2004年,2004年,2004年,2004年,2004年,2004年,2004年,2004年,2004年,2004年,2004年,2004年,2003年,2003年,b $ b 2003,2003,2003,2003,2003, 2003年,2003年,2003年,2003年,2003年,2003年,b $ b 2003,2003,2003,2002,2002,2002,2002,2002,2002,2002,2002,
2002,2002,2002,2002, 2002年,2002年,2002年,2002年,2001年,2001年,2001年,b $ b 2001年2001年2001年2001年2001年2001年2001年2001年2001年2001年2001年2001年2001年b $ b 2001年2001年2000年, 2000年,2000年,2000年,2000年,2000年,2000年, 2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,1999,1999,1999,1999,1999年,1999年,1999年,1999年,1999年,1999年,1999年,1999年,1999年, 1999年1999年1999年b $ b 1999年1998年1998年1998年1998年1998年1998年1998年1998年1998年1998年1998年1998年1998年1998年1998年1998年1998年1998年东西=结构(c(1L,
1L,2L,2L,1L,1L,1L,2L,1L,1L,1L,1L,2L,2L,1L,2L,1L,
1L,2L ,2L,1L,1L,1L,2L,1L,1L,1L,1L,2L,2L,1L,2L,1L,
1L,2L,2L,1L,1L,1L,2L,1L,1L 1L,1L,2L,2L,1L,2L,1L,1L,2L,1L,2L,2L,1L,2L,2L,1L,1L,1L,2L,1L,1L, ,
1L,2L,2L,1L,1L,1L,2L,1L,1L,1L,1L,2L,2L,1L,2L,1L,
1L,2L,2L,1L,1L ,1L,2L,1L,1L,1L,1L,2L,2L,1L,2L,1L,
1L,2L,2L,1L,1L,1L,2L,1L,1L,1L, ,2L,1L,2L,1L,
1L,2L,2L,1L,1L,1L,2L,1L,1L,1L,1L,2L,2L,1L,2L,1L,
1L ,2L,2L,1L,1L,1L,2L,1L,1L,1L,1L,2L,2L,1L,2L,1L,
1L,2L,2L,1L,1L, ,1L,1L,1L,2L,2L,1L,2L,1L,
1L,2L,2L,1L,1L,1L,2L,1L,1L,1L,1L,2L,2L,1L,2L,1L,
1L,2L,2L,1L,1L, 1L,1L,1L,1L,1L,1L,2L,2L,1L,2L,1L,
1L,2L,2L,1L,1L,1L,2L,1L, 2L,1L,2L),.Label = c(west,
east),class =factor)),.Names = c(State,BCmort,year (brewer.pal(10,Set3),
eastWest),class =data.frame,row.names = c(NA,-208L))
colVec< ),brewer.pal(6,Set3))
ltyVec <-rep(c(solid,dashed),c(10,6))
ggplot (mort3,aes(x = year,y = BCmort,col = State,lty = eastWest))+
geom_line(lwd = 1)+
scale_linetype_manual(values = c(west =solid, (),
scale_color_manual(values = c(brewer.pal(10,Set3),brewer.pal(6,Set3)))+
opts(title = BC死亡率)
xyplot(BCmort〜year,data = mort3,groups = State,lty = ltyVec,
type =l,col = colVec,lwd = 2,
key = list(lines = list(lty = ltyVec,col = colVec,lwd = 2),
text = list(l evels(mort3 $ State)),columns = 1,
space =right,title =State),grid = TRUE,main =BC death)
诀窍是映射 color
和 linetype
至 State
,然后用<16>定义 scale_linetype_manual
等级:
ggplot(mort3,aes(x = year,y = BCmort,col = State,linetype = State))+
geom_line(lwd = 1)+
scale_linetype_manual(values = c(rep(solid,10),rep(dashed,6)))+
scale_color_manual(values = c (brewer.pal(10,Set3),brewer.pal(6,Set3)))+
opts(title =BC mortality)+
theme_bw()
Background
In Germany, there are 16 federal states, ten of which belonged to West Germany, six of which belonged to East Germany. In some respects, for example mortality rates of certain cancers, there are persistent differences between the ten former western states and the six former eastern ones. There are also differences among the states within the respective groups.
To show the differences among the states, it can make a certain amount of sense to plot data, for example age-standardized breast cancer mortality by year, from each state. A plot with 16 lines is not always a good choice, and I don't want to open up a discussion about that. Sometimes the powers that be say that's how it has to be.
The problem
Differentiating among 16 lines on a plot can be difficult. To do so, I usually use a combination of colors from the RColorBrewer
package (the first ten colors of Set3
plus the first six colors of that palette again, corresponding to the ten former west and six former east states) and line types (one line type for east, one for west). Using the lattice
package, a plot of age-standardized breast cancer mortality rates from 1998-2010 by state could look like this:
The question
I would like to do a similar plot using ggplot
, but I haven't figured out how to combine the colors and the line types in the legend. So far, I've gotten this far:
If it is possible to combine colors and line types in ggplot
legends, how does one go about doing it?
Here's the code to create the data frame and the plots:
mort3 <- structure(list(State = structure(c(8L, 9L, 11L, 12L, 4L, 2L,
6L, 13L, 3L, 5L, 7L, 10L, 14L, 15L, 1L, 16L, 8L, 9L, 11L, 12L,
4L, 2L, 6L, 13L, 3L, 5L, 7L, 10L, 14L, 15L, 1L, 16L, 8L, 9L,
11L, 12L, 4L, 2L, 6L, 13L, 3L, 5L, 7L, 10L, 14L, 15L, 1L, 16L,
8L, 9L, 11L, 12L, 4L, 2L, 6L, 13L, 3L, 5L, 7L, 10L, 14L, 15L,
1L, 16L, 8L, 9L, 11L, 12L, 4L, 2L, 6L, 13L, 3L, 5L, 7L, 10L,
14L, 15L, 1L, 16L, 8L, 9L, 11L, 12L, 4L, 2L, 6L, 13L, 3L, 5L,
7L, 10L, 14L, 15L, 1L, 16L, 8L, 9L, 11L, 12L, 4L, 2L, 6L, 13L,
3L, 5L, 7L, 10L, 14L, 15L, 1L, 16L, 8L, 9L, 11L, 12L, 4L, 2L,
6L, 13L, 3L, 5L, 7L, 10L, 14L, 15L, 1L, 16L, 8L, 9L, 11L, 12L,
4L, 2L, 6L, 13L, 3L, 5L, 7L, 10L, 14L, 15L, 1L, 16L, 8L, 9L,
11L, 12L, 4L, 2L, 6L, 13L, 3L, 5L, 7L, 10L, 14L, 15L, 1L, 16L,
8L, 9L, 11L, 12L, 4L, 2L, 6L, 13L, 3L, 5L, 7L, 10L, 14L, 15L,
1L, 16L, 8L, 9L, 11L, 12L, 4L, 2L, 6L, 13L, 3L, 5L, 7L, 10L,
14L, 15L, 1L, 16L, 8L, 9L, 11L, 12L, 4L, 2L, 6L, 13L, 3L, 5L,
7L, 10L, 14L, 15L, 1L, 16L), class = "factor", .Label = c("SH",
"HH", "NI", "HB", "NW", "HE", "RP", "BW", "BY", "SL", "BE", "BB",
"MV", "SN", "ST", "TH")), BCmort = c(16.5, 16.6, 15, 14.4, 13.5,
17.1, 15.8, 16.3, 18.3, 16.8, 17, 18.1, 13.1, 15.1, 18.8, 13.1,
16.4, 16.1, 15.8, 12.8, 16.3, 19.2, 16.8, 13, 17.9, 17, 19.4,
19.4, 13.1, 13.8, 18.1, 13.8, 15.9, 17.3, 17.5, 13.7, 17.4, 17.5,
16.7, 15.5, 18.1, 18, 20.1, 19.1, 11.8, 14.6, 18.2, 13.4, 16.8,
17.5, 15.6, 14.1, 13.9, 18.2, 17.1, 15.2, 18.1, 16.6, 19.3, 18.6,
13.1, 14.6, 19.6, 12.4, 16.6, 17.8, 17.5, 14.3, 20.5, 19.2, 19,
12.6, 19.5, 17.8, 19.2, 21, 14.4, 13.4, 19.8, 14, 17.5, 18.9,
16.4, 14.7, 17.7, 20.1, 18.5, 14.5, 19.1, 19.2, 20.1, 19.7, 14.2,
16.2, 17.9, 12.6, 18, 18.7, 17.7, 16.5, 16.6, 20.3, 18.1, 15.2,
19, 20, 19.8, 21.3, 13.8, 14.8, 20.4, 14.8, 18.2, 18.7, 16.9,
16.2, 20.2, 20.4, 18.5, 14, 20.2, 18.7, 20.3, 17.7, 14.4, 14.5,
21.7, 13.7, 18.3, 19.7, 17.8, 16.5, 20.2, 21.7, 18.8, 16.7, 20.4,
20, 19.6, 22.9, 15.2, 14.9, 21.7, 14.6, 18.3, 19.7, 17, 16.7,
22.9, 16.2, 19.6, 15.9, 20.3, 19.9, 18.9, 21.8, 14.9, 18, 21.4,
16.1, 19.6, 19.2, 19.1, 16.7, 20, 18.2, 20.5, 15.5, 20.5, 21.1,
21.3, 23.8, 15.8, 15.3, 21.3, 15.7, 19.6, 20.3, 19.2, 17.4, 18.1,
23.1, 20.6, 16.2, 21.5, 20.3, 21.4, 20.8, 16.1, 15.8, 22.1, 14.5,
20, 20.2, 19, 18.7, 23.1, 21.8, 19.4, 17.4, 20.9, 20.5, 20.4,
23.2, 16.3, 17.6, 23.1, 16.5), year = c(2010, 2010, 2010, 2010,
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010,
2010, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009,
2009, 2009, 2009, 2009, 2009, 2009, 2008, 2008, 2008, 2008, 2008,
2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2008,
2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007,
2007, 2007, 2007, 2007, 2007, 2006, 2006, 2006, 2006, 2006, 2006,
2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2005,
2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005,
2005, 2005, 2005, 2005, 2004, 2004, 2004, 2004, 2004, 2004, 2004,
2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2003, 2003,
2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003,
2003, 2003, 2003, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002,
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2001, 2001, 2001,
2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001,
2001, 2001, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000,
2000, 2000, 2000, 2000, 2000, 2000, 2000, 1999, 1999, 1999, 1999,
1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999,
1999, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998,
1998, 1998, 1998, 1998, 1998, 1998), eastWest = structure(c(1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L), .Label = c("west",
"east"), class = "factor")), .Names = c("State", "BCmort", "year",
"eastWest"), class = "data.frame", row.names = c(NA, -208L))
colVec<-c(brewer.pal(10,"Set3"),brewer.pal(6,"Set3"))
ltyVec<-rep(c("solid","dashed"),c(10,6))
ggplot(mort3, aes(x = year, y = BCmort, col = State, lty = eastWest)) +
geom_line(lwd = 1) +
scale_linetype_manual(values = c(west = "solid", east = "dashed")) +
scale_color_manual(values = c(brewer.pal(10, "Set3"), brewer.pal(6, "Set3"))) +
opts(title = "BC mortality")
xyplot(BCmort ~ year, data = mort3, groups = State, lty = ltyVec,
type = "l", col = colVec, lwd = 2,
key = list(lines = list(lty = ltyVec, col = colVec, lwd = 2),
text = list(levels(mort3$State)), columns = 1,
space = "right", title = "State"), grid = TRUE, main = "BC mortality")
The trick is to map both colour
and linetype
to State
, and then to define scale_linetype_manual
with 16 levels:
ggplot(mort3, aes(x = year, y = BCmort, col = State, linetype = State)) +
geom_line(lwd = 1) +
scale_linetype_manual(values = c(rep("solid", 10), rep("dashed", 6))) +
scale_color_manual(values = c(brewer.pal(10, "Set3"), brewer.pal(6, "Set3"))) +
opts(title = "BC mortality") +
theme_bw()
这篇关于控制ggplot图例中的线条颜色和线条类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!