【问题标题】:Bar plot with two factors with gradient color in RR中具有渐变颜色的两个因子的条形图
【发布时间】:2016-08-04 11:54:01
【问题描述】:

我有一个这样的数据框:

df = data.frame(time = rep(0.5:9.5, each = 10), roi = rep(1:10, 10), area = runif(100, 5.0, 7.5))

我想绘制一个图,包括 10 个条形图,其中包含 timeroi 的 10 个值。在这种情况下,x 轴为roi,y 轴为timearea 的值将以渐变颜色显示(最高 = 白色,最低 = 黑色)。 y 轴从上 0.5 反转到下 9.5,条的宽度为 2。

预期的数字看起来像 this image.

期待您的回复,感谢您的帮助。

【问题讨论】:

  • 你能解释一下如果你用手(纸+彩色铅笔)画一个条(假设是在上面的那个,在time == 0.5旁边)吗?
  • 对不起,我对数据框有点困惑,你能把它改成 df=data.frame(time=rep(0.5:9.5,each=10),roi=rep(1:10) ,10),area=runif(100, 5.0, 7.5))
  • @Qaswed:我在这里找到了一个名为“yAxisReversed”的选项 ggplot2.barplot sthda.com/english/wiki/…

标签: r ggplot2 bar-chart gradient factors


【解决方案1】:

这是你的想法吗?

df=data.frame(time=as.factor(rep(0.5:9.5,each=10)),roi=rep(1:10,10),area=runif(100, 5.0, 7.5))

df$time <- factor(df$time, levels=rev(levels(df$time)))

ggplot(data=df, aes(y=factor(roi), x=time, fill = area)) +
  theme_minimal()  + coord_flip() +
  geom_tile(colour = "white", width = .9, height = 1) + 
  scale_fill_gradient(low="blue",high="red")

【讨论】:

  • 是的,这正是我所需要的。我非常爱你和你的回答。我在拍手。非常感谢。
  • 嗨 Shirin Glander,我想减小以下条形的宽度(实际上是高度):df=data.frame(time=as.factor(rep(0.5:9.5,each=10)) ,roi=rep(1:10,10),area=runif(100, 5.0, 7.5)) df$time
  • 调整 geom_tile(color = "white", width = .9, height = 1.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 2020-12-31
  • 2014-09-18
  • 1970-01-01
  • 2023-03-19
相关资源
最近更新 更多