【发布时间】:2020-12-16 03:37:28
【问题描述】:
我试图在R Cookbook 复制一个示例,该示例在图例中指定颜色和形状。
原代码是
# A different data set
df2 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(rep(c(1,2),2), levels=c(1,2)),
total_bill = c(13.53, 16.81, 16.24, 17.42)
)
# A basic graph
ggplot(data=df2, aes(x=time, y=total_bill, group=sex, shape=sex)) +
geom_line() +
geom_point() +
scale_colour_discrete(name ="Payer",
breaks=c("Female", "Male"),
labels=c("Woman", "Man")) +
scale_shape_discrete(name ="Payer",
breaks=c("Female", "Male"),
labels=c("Woman", "Man"))
我不想要这条线,所以我注释掉了geom_line()+ 部分。点不再有颜色。有人可以帮忙吗?
# A different data set
df2 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(rep(c(1,2),2), levels=c(1,2)),
total_bill = c(13.53, 16.81, 16.24, 17.42)
)
# A basic graph
ggplot(data=df2, aes(x=time, y=total_bill, group=sex, shape=sex)) +
# geom_line() +
geom_point() +
scale_colour_discrete(name ="Payer",
breaks=c("Female", "Male"),
labels=c("Woman", "Man")) +
scale_shape_discrete(name ="Payer",
breaks=c("Female", "Male"),
labels=c("Woman", "Man"))
【问题讨论】:
-
您的代码中似乎缺少
colour美学,因此这两个版本都不应首先具有颜色。
标签: r ggplot2 data-visualization