我尝试从其他一些解决方案适应略有不同的情况。我无法解决这个问题。
我想构建一个镜像的小号,将化学物质与对照组进行比较,但结果由化学浓度进行分组,并且(如果可能的话)两个正轴。
我在下面提供数据,这是我希望它看起来像这样的例子。
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).

Thanks in advance!
发布评论
评论(2)
尝试以下尝试:
我怀疑左轴上的顺序(最初是
x
,但用coord_flip
向左翻转将与您相关。如果当前不是您需要的,并且使用交互(L1,volatile)
而不是给您正确的订单,那么您将需要在 <之前智能地组合它们。代码> ggplot(..),转换为factor
,并控制Levels =
,以便它们以顺序(和字符串形式)为准。需要。大多数其他方面都可以通过
+主题(...)
来控制,例如legend.position =“ top”
。我不知道演示图像中的星号可能是什么,但是可以使用geom_point
添加它们(请确保否定应该在左边的)。例如,如果您有一个
$ star
变量,该变量表明每个特定行上应该有一颗星,则可以使用一个
geom_point
call(并添加添加)传奇动作):Try this:
I suspect that the order on the left axis (originally
x
, but flipped to the left withcoord_flip
) will be relevant to you. If the current isn't what you need and usinginteraction(L1, volatile)
instead does not give you the correct order, then you will need to combine them intelligently beforeggplot(..)
, convert to afactor
, and control thelevels=
so that they are in the order (and string-formatting) you need.Most other aspects can be controlled via
+ theme(...)
, such aslegend.position="top"
. I don't know what the asterisks in your demo image might be, but they can likely be added withgeom_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,then you can add it with a single
geom_point
call (and adding the legend move):就像R2evans所述,这样做的唯一方法是在标签时使用
abs()
在数据中使用负值。更具体地说,它看起来像这样:当然,请使用任何标签对数据最有意义,或者您可以使用
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: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!