ggplot2,与aes映射

时间:2018-05-02 16:05:29

标签: r ggplot2

我为每一行指定线型,​​结果实心变为点状,点状变为实心。我尝试使用实线,虚线和点线,但仍然无法弄清楚绘图中显示的线型的顺序。以下是代码:

library(ggplot2)
library(ggthemes)

p = ggplot(data = msleep, aes(x = log(bodywt), y = sleep_total)) + 
  geom_point() + #' This time we will vary the feeding groups by shapes instead of colors
  geom_smooth(method='lm')

gg_data <- ggplot_build(p)


sleepplot2 = ggplot(data = msleep, aes(x = log(bodywt), y = sleep_total)) + 
  geom_point(shape=4, size=3, color='blue') + #' This time we will vary the feeding groups by shapes instead of colors
  geom_text(aes(label=name), nudge_x = 0.5, nudge_y = 1) +
  labs(x = "Log body weight (Kg)", y = "Time asleep (hrs/day)") +
  ggtitle('Housing 1990-2007') +
  geom_line(color='red', data = gg_data$data[[2]], mapping=aes(x = x, y = ymin, linetype='solid'), size = 0.5) +
  geom_smooth(color='red',data = msleep, mapping=aes(x = log(bodywt), y = sleep_total, linetype='dotted'), method='lm', se = FALSE, size=0.5) +
  geom_line(color='red', data = gg_data$data[[2]], mapping=aes(x = x, y = ymax, linetype='solid'), size = 0.5) +
  scale_linetype_manual('', values = c("solid","dotted"),labels=c("Fit: y=0.3*x", "95% conf. bounds"))

1 个答案:

答案 0 :(得分:0)

运行以下代码:(与不使用ggtheme包的代码相同)

library(ggplot2)

ggplot(data = msleep, aes(x = log(bodywt), y = sleep_total)) + 
  geom_point() +
  geom_smooth(method='lm')

ggplot(data = msleep, aes(x = log(bodywt), y = sleep_total)) + 
  geom_point(shape=4, size=3, color='blue') +
  geom_text(aes(label=name), nudge_x = 0.5, nudge_y = 1) +
  labs(x = "Log body weight (Kg)", y = "Time asleep (hrs/day)") +
  ggtitle('Housing 1990-2007') +
  geom_line(color='red', data = gg_data$data[[2]], mapping=aes(x = x, y = ymin, linetype='solid'), size = 0.5) +
  geom_smooth(color='red',data = msleep, mapping=aes(x = log(bodywt), y = sleep_total, linetype='dotted'), method='lm', se = FALSE, size=0.5) +
  geom_line(color='red', data = gg_data$data[[2]], mapping=aes(x = x, y = ymax, linetype='solid'), size = 0.5) +
  scale_linetype_manual('', values = c("dotted","solid"),labels=c("Fit: y=0.3*x", "95% conf. bounds"))

给我们以下情节:

plot needed

情节以及情节中的三条线似乎都同意你想要实现的情节。