创建一个镜像的,分组的Barplot?

发布于 2025-01-21 17:55:27 字数 1114 浏览 4 评论 0 原文

我尝试从其他一些解决方案适应略有不同的情况。我无法解决这个问题。

我想构建一个镜像的小号,将化学物质与对照组进行比较,但结果由化学浓度进行分组,并且(如果可能的话)两个正轴。

我在下面提供数据,这是我希望它看起来像这样的例子。

volatiles<-c("hexenal3", "trans2hexenal", "trans2hexenol", "ethyl2hexanol", "phenethylalcohol", "methylsalicylate", "geraniol", "eugenol")


require(reshape2)
dat<-list(
conc1=data.frame("volatile"=volatiles, "focal"=c(26,27,28,28,31,31,30,28), "control"=c(24,31,30,29,24,23,21,25)),
conc2=data.frame("volatile"=volatiles, "focal"=c(29,18,34,17,30,32,35,27), "control"=c(21,42,20,40,25,16,17,29)),
conc3=data.frame("volatile"=volatiles, "focal"=c(33, 5,38, 7,37,35,40,26), "control"=c(18,51,14, 50,15,12,13,31))
)
long.dat<-melt(dat)

尝试以下内容不起作用。也许我应该输入不同的数据结构?

ggplot(long.dat, aes(x=L1, group=volatile, y=value, fill=variable)) + 
geom_bar(stat="identity", position= "identity")

我希望它看起来与此相似,但是将棒分组为不同浓度的三合会(如果可能的话,所有正值)。

提前致谢!

I have attempted adapting from some other solutions for slightly different situations. I am not being able to sort this out.

I would like to build a mirrored barplot comparing chemicals with controls, but with results grouped by chemical concentrations, and (if possible) both positive axes.

I provide data below, and an example of I would like it to generally look like.

volatiles<-c("hexenal3", "trans2hexenal", "trans2hexenol", "ethyl2hexanol", "phenethylalcohol", "methylsalicylate", "geraniol", "eugenol")


require(reshape2)
dat<-list(
conc1=data.frame("volatile"=volatiles, "focal"=c(26,27,28,28,31,31,30,28), "control"=c(24,31,30,29,24,23,21,25)),
conc2=data.frame("volatile"=volatiles, "focal"=c(29,18,34,17,30,32,35,27), "control"=c(21,42,20,40,25,16,17,29)),
conc3=data.frame("volatile"=volatiles, "focal"=c(33, 5,38, 7,37,35,40,26), "control"=c(18,51,14, 50,15,12,13,31))
)
long.dat<-melt(dat)

Attempting the following isn't working. Perhaps I should input a different data structure?

ggplot(long.dat, aes(x=L1, group=volatile, y=value, fill=variable)) + 
geom_bar(stat="identity", position= "identity")

I would like it to look similar to this, but with the bars grouped in the triads of different concentrations (and, if possible, with all positive values).

data plot from another software
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独岁月 2025-01-28 17:55:27

尝试以下尝试:

long.dat$value[long.dat$variable == "focal"] <-  -long.dat$value[long.dat$variable == "focal"]

library(ggplot2)
gg <- ggplot(long.dat, aes(interaction(volatile, L1), value)) +
  geom_bar(aes(fill = variable), color = "black", stat = "identity") +
  scale_y_continuous(labels = abs) +
  scale_fill_manual(values = c(control = "#00000000", focal = "blue")) +
  coord_flip()
gg

“

我怀疑左轴上的顺序(最初是 x ,但用 coord_flip 向左翻转将与您相关。如果当前不是您需要的,并且使用交互(L1,volatile)而不是给您正确的订单,那么您将需要在 <之前智能地组合它们。代码> ggplot(..),转换为 factor ,并控制 Levels = ,以便它们以顺序(和字符串形式)为准。需要。

大多数其他方面都可以通过+主题(...)来控制,例如 legend.position =“ top” 。我不知道演示图像中的星号可能是什么,但是可以使用 geom_point 添加它们(请确保否定应该在左边的)。

例如,如果您有一个 $ star 变量,该变量表明每个特定行上应该有一颗星,

set.seed(42)
long.dat$star <- sample(c(TRUE,FALSE), prob=c(0.2,0.8), size=nrow(long.dat), replace=TRUE)
head(long.dat)
#           volatile variable value    L1  star
# 1         hexenal3    focal   -26 conc1  TRUE
# 2    trans2hexenal    focal   -27 conc1  TRUE
# 3    trans2hexenol    focal   -28 conc1 FALSE
# 4    ethyl2hexanol    focal   -28 conc1  TRUE
# 5 phenethylalcohol    focal   -31 conc1 FALSE
# 6 methylsalicylate    focal   -31 conc1 FALSE

则可以使用一个 geom_point call(并添加添加)传奇动作):

gg +
  geom_point(aes(y=value + 2*sign(value)), data = ~ subset(., star), pch = 8) +
  theme(legend.position = "top")

“带有星星和重新定位传奇的同一GGPLOT”

Try this:

long.dat$value[long.dat$variable == "focal"] <-  -long.dat$value[long.dat$variable == "focal"]

library(ggplot2)
gg <- ggplot(long.dat, aes(interaction(volatile, L1), value)) +
  geom_bar(aes(fill = variable), color = "black", stat = "identity") +
  scale_y_continuous(labels = abs) +
  scale_fill_manual(values = c(control = "#00000000", focal = "blue")) +
  coord_flip()
gg

ggplot2 bar plot, grouped and filled by 'variable', and 'focal' data to the left and 'control' data to the right of center

I suspect that the order on the left axis (originally x, but flipped to the left with coord_flip) will be relevant to you. If the current isn't what you need and using interaction(L1, volatile) instead does not give you the correct order, then you will need to combine them intelligently before ggplot(..), convert to a factor, and control the levels= so that they are in the order (and string-formatting) you need.

Most other aspects can be controlled via + theme(...), such as legend.position="top". I don't know what the asterisks in your demo image might be, but they can likely be added with geom_point (making sure to negate those that should be on the left).

For instance, if you have a $star variable that indicates there should be a star on each particular row,

set.seed(42)
long.dat$star <- sample(c(TRUE,FALSE), prob=c(0.2,0.8), size=nrow(long.dat), replace=TRUE)
head(long.dat)
#           volatile variable value    L1  star
# 1         hexenal3    focal   -26 conc1  TRUE
# 2    trans2hexenal    focal   -27 conc1  TRUE
# 3    trans2hexenol    focal   -28 conc1 FALSE
# 4    ethyl2hexanol    focal   -28 conc1  TRUE
# 5 phenethylalcohol    focal   -31 conc1 FALSE
# 6 methylsalicylate    focal   -31 conc1 FALSE

then you can add it with a single geom_point call (and adding the legend move):

gg +
  geom_point(aes(y=value + 2*sign(value)), data = ~ subset(., star), pch = 8) +
  theme(legend.position = "top")

same ggplot with stars and relocated legend

dawn曙光 2025-01-28 17:55:27

就像R2evans所述,这样做的唯一方法是在标签时使用 abs()在数据中使用负值。更具体地说,它看起来像这样:

ggplot(long.dat, aes(x=L1, group=volatile, y=value, fill=variable)) + 
geom_bar(stat="identity", position= "identity") +
scale_y_continuous(breaks= c(-25,-15,-5,5,15,25),labels=abs(c(-25,-15,-5,5,15,25)))

当然,请使用任何标签对数据最有意义,或者您可以使用 seq()函数设置数字序列。

PS我也遇到了您的代码问题,因此下次请确保您的示例可再现 - 您会更快地获得答案!

Like r2evans stated, the only way to do this is to use negative values in your data, and then manually use abs() when labelling. More specifically, it would look something like this:

ggplot(long.dat, aes(x=L1, group=volatile, y=value, fill=variable)) + 
geom_bar(stat="identity", position= "identity") +
scale_y_continuous(breaks= c(-25,-15,-5,5,15,25),labels=abs(c(-25,-15,-5,5,15,25)))

Of course, use whatever labels make the most sense for your data, or you can set a sequence of numbers using the seq() function.

P.S. I also had trouble with your code, so next time please make sure your example is reproducible- you'll get answers quicker!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文