当shape<时,带有scale_shape_manual的ggplot颜色

时间:2015-11-25 22:51:57

标签: r ggplot2

我在CSV文件中有数据:

Year,A,B,C,D,E,F,G
2007,3.35,,,,,,
2008,3.54,3.59,,,,,
2009,3.22,3.46,4.43,,,,
2010,3.82,3.63,4.64,,,,
2011,2.91,3.74,4.5,4.13,4.38,,
2012,3.85,3.57,4.13,4,4,4,
2013,4.33,2.93,4.63,4.71,4.25,,
2014,4.73,4,4.81,4.66,4.33,,4
2015,,,4.89,4.68,,,

我试图像这样绘制:

scores_raw = read.csv("scores.csv", header = TRUE, fill = TRUE)

scores_melt <- melt(scores_raw, id = "Year")

scores_symb <- c(15, 17, 16, 16, 16, 16, 16)  

plot_scores <- ggplot(scores_melt, aes(x=Year, y=value, colour=variable, shape=variable))
plot_scores +
  geom_line() + 
  geom_point(size = 10, alpha = 0.6) + 
  scale_shape_manual(values = scores_symb, 
                     name="Cohort\nSize",
                     labels=c("200", "100", "25")) +
  ylab("Score (5 = max)") + 
  scale_y_continuous(limits = c(0, 5)) +
  theme_bw() +
  theme(
    text = element_text(size=30)
    , axis.title.y=element_text(vjust=1.5)
    , axis.title.x=element_text(vjust=0.1)
    , plot.background = element_rect(fill = "transparent",colour = NA)
    , legend.justification=c(0,0), legend.position=c(0,0) #legend.position="none"
    , legend.background = element_rect(fill="transparent", size=.5, linetype="dotted")
  )

正如你所知,我有7个系列,但只想通过3个队列大小(即形状)区分它们。

我希望这个图例只显示区分我所获得的三种数据的三种形状。目前,我可以制作一个形状和颜色相结合的单个图例。或者两个传说(如上面的代码所示),它们产生两个图例,一个带有形状(其中4个是NA),另一个带有颜色。

请帮忙!

1 个答案:

答案 0 :(得分:1)

您正尝试将群组大小映射到shape。实际上,将群组大小映射到shape(而不是variable),而不是黑客攻击。您可以通过创建名为cohort_size的新变量来实现此目的。

读入数据

scores_raw <- read.table(text = "Year,A,B,C,D,E,F,G
2007,3.35,,,,,,
2008,3.54,3.59,,,,,
2009,3.22,3.46,4.43,,,,
2010,3.82,3.63,4.64,,,,
2011,2.91,3.74,4.5,4.13,4.38,,
2012,3.85,3.57,4.13,4,4,4,
2013,4.33,2.93,4.63,4.71,4.25,,
2014,4.73,4,4.81,4.66,4.33,,4
2015,,,4.89,4.68,,,", sep = ",", header = TRUE)

融化并创建新变量

scores_symb <- c(15, 17, 16, 16, 16, 16, 16)
scores_melt <- reshape2::melt(scores_raw, id = "Year")
# Add the new variable
scores_melt$cohort_size <- scores_melt$variable
# Correctly map the levels
levels(scores_melt$cohort_size) <- scores_symb
# Reorder the levels (you might want to use lables = c(25, 100, 200) here)
scores_melt$cohort_size <- factor(scores_melt$cohort_size, levels = 15:17)

创建情节

# Simplified ggplot call that looks ok on my screen
ggplot(scores_melt, aes(x = Year, y = value, colour = variable, shape = cohort_size)) +
  geom_line() +
  geom_point(size = 5, alpha = 0.6) +
  ylab("Score (5 = max)") +
  scale_colour_discrete(guide = FALSE) +
  theme_bw() +
  theme(legend.position=c(0.1, 0.8))

结果

enter image description here