[英]Theme elements in ggplot2 figure
我正在嘗試調整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
謝謝莎拉
你不遠。
對於點1:代替xlab
,使用scale_x_continuous
,設置breaks
並指定一個空標題。
對於點2:將fill = Species
從全局aes
函數中移出到geom_bar
的aes
函數中。
對於第3點:在scale_shape
和scale_linetype
函數中更改圖例標題,確保兩個標題都相同。
對ggplot的代碼進行以下更改:
plot_x = ggplot(my.df, aes(Year, Inf.ct)) +
geom_bar(aes(fill=Species), stat="identity", position="dodge") +
ylab(expression("Number of stems (dbh">="1-cm)", sep="")) +
scale_x_continuous("", breaks = seq(2004, 2011, 1)) +
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)) +
scale_shape("SOD-dead Stems") +
scale_linetype("SOD-dead Stems")
plot_x
給出以下圖:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.