我试图在ggplot2图中调整一些主题元素。我对这个多层次数字的三个问题/目标是(数据代码结束):
我的代码对你们许多人来说可能是多余的,所以随时随地提供建议,例如只有一个图例。我是ggplot的新手(喜欢它)但到目前为止还没有“鳞片”......
我的代码:
##### BUILD UP DATAFRAME:
quag.infect= c(31, 52, 58, 74, 76, 85, 99, 102)
quke.infect= c(10, 13, 17, 20, 23, 27, 28, 27)
qusp.hosts = (rep(c("QUAG", "QUKE"), each=8))
year = rep(2004:2011, 2)
quag.dead = c(NA, 4, 11, 19, 33, 38, 48, 49)
quke.dead = c(NA, 1, 1, 1, 2, 3, 7, 8)
my.df = data.frame(Species=qusp.hosts, Year=year,
Inf.ct = c(quag.infect, quke.infect), Sod.Dead=c(quag.dead, quke.dead))
##### Establish grey colors for bars:
grays = c(QUAG="gray50", QUKE="gray66")
##### Make multi-layered graphic:
library(ggplot2)
plot_x = ggplot(my.df, aes(Year, Inf.ct, fill=Species)) +
geom_bar(stat="identity", position="dodge") +
ylab(expression("Number of stems (dbh">="1-cm)", sep="")) +
xlab("") +
scale_fill_manual(values=grays, "Infected Stems", labels = c("Black Oak", "Coast Live Oak")) +
geom_line(aes(y=Sod.Dead, linetype=Species)) +
geom_point(aes(y=Sod.Dead, shape=Species))
plot_x
谢谢, 萨拉
答案 0 :(得分:1)
你离我不远。
对于第1点:取代xlab
,使用scale_x_continuous
,设置breaks
,并指定空标题。
对于第2点:将fill = Species
移出全局aes
函数,转换为aes
的{{1}}函数。
对于第3点:更改geom_bar
和scale_shape
函数中的图例标题,确保同一标题同时包含在内。
对ggplot的代码进行这些更改:
scale_linetype
给出以下图: