设置ggplot2中不同叠加图中的组别形状类型

huangapple go评论48阅读模式
英文:

Setting shape types in groups with different overlay plots in ggplot2

问题

I am hoping to fine-tune ggplot2 shape types in two overlay plots when coding by groups. Is this possible? I created a strip plot with points for mean observations, coded with two different shapes for two different factors of seeding rate, and three different colors for different fertilizer price scenarios. I included jitter observations of the values used to calculate these means, also coded by the same color and shape groups listed for the mean points. I included lines showing standard errors for each mean.
To improve visualization, I would like the jitter observations to be open shapes and the means to be closed shapes.

library(ggplot2)
p <- ggplot() + geom_jitter(data=econ4, position=position_jitter(0.2), aes(x=location, y=profit, shape=seeding.rate, colour=urea.cost.USD, alpha=0.05)) + theme_bw() + geom_hline(yintercept=0) + labs(y = "profit per ha (USD)")
p <- p + geom_point(data=econ4, aes(x=location, y=mean.profit, colour=urea.cost.USD, shape=seeding.rate))
p <- p + geom_linerange(data=econ4, aes(x=location, y=mean.profit, ymin=mean.profit-se.profit, ymax=mean.profit+se.profit, colour=urea.cost.USD)) 
#p <- p + scale_shape_manual(values=1:2)
p <- p + scale_colour_manual(values=c("#0072B2", "#009E73", "#CC79A7"))
p <- p + coord_flip()
p <- p + scale_shape_manual(values=1:2) #this changes all shapes to open. I would like this to apply only to the geom_jitter.

设置ggplot2中不同叠加图中的组别形状类型

英文:

I am hoping to fine tune ggplot2 shape types in two overlay plots when coding by groups. Is this possible? I created a strip plot with points for mean observations, coded with two different shapes for two different factors of seeding rate, and three different colors for different fertilizer price scenarios. I included jitter observations of the values used to calculate these means, also coded by the same color and shape groups listed for the mean points. I included lines showing standard errors for each mean.
To improve visualization, I would like the jitter observations to be open shapes and the means to be closed shapes.

library(ggplot2)
p&lt;-ggplot()+  geom_jitter(data=econ4,position=position_jitter(0.2),aes(x=location,y=profit,shape=seeding.rate,colour=urea.cost.USD,alpha=0.05))+theme_bw()+geom_hline(yintercept=0)+labs(y = &quot;profit per ha (USD)&quot;)
p&lt;-p+geom_point(data=econ4,aes(x=location,y=mean.profit,colour=urea.cost.USD,shape=seeding.rate))
p&lt;-p+geom_linerange(data=econ4,aes(x=location,y=mean.profit,ymin=mean.profit-se.profit,ymax=mean.profit+se.profit,colour=urea.cost.USD)) 
#p&lt;-p+scale_shape_manual(values=1:2)
p&lt;-p+scale_colour_manual(values=c(&quot;#0072B2&quot;,&quot;#009E73&quot;, &quot;#CC79A7&quot;))
p&lt;-p + coord_flip()
p&lt;-p+scale_shape_manual(values=1:2) #this changes all shapes to open.  I would like this to apply only to the geom_jitter.

设置ggplot2中不同叠加图中的组别形状类型

答案1

得分: 0

如您所需,以下是代码部分的翻译:

library(ggplot2)

set.seed(123)

econ4 <- data.frame(
  location = sample(LETTERS[1:5], 100, replace = TRUE),
  urea.cost.USD = sample(c("300", "550", "1000"), 100, replace = TRUE),
  seeding.rate = sample(c("Mirsky", "Poffenbarger"), 100, replace = TRUE),
  profit = rlnorm(100, 4) - 100
)

ggplot(econ4, aes(profit, location, colour = urea.cost.USD)) +
  geom_vline(xintercept = 0) +
  geom_jitter(
    position = position_jitter(0.2),
    aes(shape = paste0(seeding.rate, ".jitter")), alpha = .8,
    size = 2
  ) +
  geom_point(aes(shape = paste0(seeding.rate, ".mean")),
    stat = "summary", fun = mean, size = 2
  ) +
  geom_linerange(stat = "summary", fun.data = "mean_se") +
  scale_colour_manual(
    values = c("#0072B2", "#009E73", "#CC79A7")
  ) +
  scale_shape_manual(
    values = c(Mirsky.jitter = 16, Mirsky.mean = 21, Poffenbarger.jitter = 17, Poffenbarger.mean = 24),
    breaks = c("Mirsky.jitter", "Poffenbarger.jitter"),
    labels = c("Minsky", "Poffenbarger")
  ) +
  theme_bw() +
  labs(x = "每公顷利润(美元)", shape = "播种速率")

设置ggplot2中不同叠加图中的组别形状类型<!-- -->

英文:

As you want different shapes for jittered and mean points mapping seeding.rate on shape isn't sufficient. Instead you have to "recode" seeding.rate to differntiate between jittered and mean points and to get four different shapes. To this end you could map e.g. paste0(seeding.rate, &quot;.jitter&quot;) on the shape aes in geom_jitter and paste0(seeding.rate, &quot;.mean&quot;) on the shape aes in geom_point. As a result we have four different categories to which we could assign your desired four different shapes.

Additionally I slightly refactored your code and instead of computing the mean and se manually I use stat=&quot;summary&quot; to compute the statistics on the fly.

Using some fake random example data:

library(ggplot2)

set.seed(123)

econ4 &lt;- data.frame(
  location = sample(LETTERS[1:5], 100, replace = TRUE),
  urea.cost.USD = sample(c(&quot;300&quot;, &quot;550&quot;, &quot;1000&quot;), 100, replace = TRUE),
  seeding.rate = sample(c(&quot;Mirsky&quot;, &quot;Poffenbarger&quot;), 100, replace = TRUE),
  profit = rlnorm(100, 4) - 100
)

ggplot(econ4, aes(profit, location, colour = urea.cost.USD)) +
  geom_vline(xintercept = 0) +
  geom_jitter(
    position = position_jitter(0.2),
    aes(shape = paste0(seeding.rate, &quot;.jitter&quot;)), alpha = .8,
    size = 2
  ) +
  geom_point(aes(shape = paste0(seeding.rate, &quot;.mean&quot;)),
    stat = &quot;summary&quot;, fun = mean, size = 2
  ) +
  geom_linerange(stat = &quot;summary&quot;, fun.data = &quot;mean_se&quot;) +
  scale_colour_manual(
    values = c(&quot;#0072B2&quot;, &quot;#009E73&quot;, &quot;#CC79A7&quot;)
  ) +
  scale_shape_manual(
    values = c(Mirsky.jitter = 16, Mirsky.mean = 21, Poffenbarger.jitter = 17, Poffenbarger.mean = 24),
    breaks = c(&quot;Mirsky.jitter&quot;, &quot;Poffenbarger.jitter&quot;),
    labels = c(&quot;Minsky&quot;, &quot;Poffenbarger&quot;)
  ) +
  theme_bw() +
  labs(x = &quot;profit per ha (USD)&quot;, shape = &quot;seeding rate&quot;)

设置ggplot2中不同叠加图中的组别形状类型<!-- -->

huangapple
  • 本文由 发表于 2023年4月20日 03:01:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定