1

我正在尝试调整 ggplot2 图中的一些主题元素。我对这个多层图的三个问题/目标是(数据代码如下):

  1. 为所有八年创建一个 x 轴刻度标记标签,即 2004-2011。我不想要一个'xlab'。
  2. 从“受感染的茎”图例 b/c 中删除两个实心点,这些“geom_bars”不应该有与之关联的点 (???)。
  3. 将线/点图例的图例标题更改为“SOD-dead Stems”。

我的代码对你们中的许多人来说可能是多余的,所以请随时在任何地方提供建议,例如只有一个图例。我是 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 

谢谢,莎拉

4

1 回答 1

1

你不远了。

对于第 1 点:代替xlab,使用scale_x_continuous,设置breaks,并指定一个空标题。

对于第 2 点:fill = Species从全局aes函数移出到aes函数 for geom_bar

对于第 3 点:更改scale_shapescale_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 

给出以下情节:

在此处输入图像描述

于 2012-11-26T07:29:53.337 回答