简体   繁体   English

ggplot2使用位置躲避2时堆叠的带类别和数值的条形图

[英]ggplot2 stacked bars with categorial and numerical values while using position dodge2

I have a dataframe with certain factors and numerical values that I want to plot. 我有一个数据框,其中包含要绘制的某些因子和数值。

The data that I have looks exactly like this (can't reproduce a sample sorry): 我拥有的数据看起来完全像这样(无法重现示例):

    library(ggplot2)

df=structure(list(StartPos = c(6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
       6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 
       8, 8, 8, 8), Direction = c("Left", "Left", "Left", "Left", "Left", 
       "Left", "Left", "Left", "Left", "Right", "Right", "Right", "Right", 
       "Right", "Right", "Right", "Right", "Right", "Left", "Left", 
       "Left", "Left", "Left", "Left", "Left", "Left", "Left", "Right", 
       "Right", "Right", "Right", "Right", "Right", "Right", "Right", 
       "Right"), Velocity = c(36, 36, 36, 36, 36, 36, 36, 36, 36, -36, 
       -36, -36, -36, -36, -36, -36, -36, -36, 36, 36, 36, 36, 36, 36, 
       36, 36, 36, -36, -36, -36, -36, -36, -36, -36, -36, -36), Duration = c(0.2, 
       0.2, 0.2, 0.5, 0.5, 0.5, 1, 1, 1, 0.2, 0.2, 0.2, 0.5, 0.5, 0.5, 
       1, 1, 1, 0.2, 0.2, 0.2, 0.5, 0.5, 0.5, 1, 1, 1, 0.2, 0.2, 0.2, 
       0.5, 0.5, 0.5, 1, 1, 1), n_runs = c(12, 12, 12, 12, 12, 12, 12, 
       12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
       12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12), Response = c("H", 
       "M", "W", "H", "M", "W", "H", "M", "W", "H", "M", "W", "H", "M", 
       "W", "H", "M", "W", "H", "M", "W", "H", "M", "W", "H", "M", "W", 
       "H", "M", "W", "H", "M", "W", "H", "M", "W"), n_hits = c(8, 1, 
       3, 10, 1, 1, 10, 2, 0, 10, 2, 0, 11, 1, 0, 10, 2, 0, 8, 3, 1, 
       9, 0, 3, 9, 3, 0, 10, 2, 0, 10, 2, 0, 12, 0, 0), p_test = c(0.66666667, 
       0.08333333, 0.25, 0.83333333, 0.08333333, 0.08333333, 0.83333333, 
       0.16666667, 0, 0.83333333, 0.16666667, 0, 0.91666667, 0.08333333, 
       0, 0.83333333, 0.16666667, 0, 0.66666667, 0.25, 0.08333333, 0.75, 
       0, 0.25, 0.75, 0.25, 0, 0.83333333, 0.16666667, 0, 0.83333333, 
       0.16666667, 0, 1, 0, 0)), class = "data.frame", row.names = c(NA, 
       -36L))

The goal that I want to achieve is, plotting the StartPos and their directions, durations and percentages (H, M, W ; Hit, Miss, Wrong) in a certain way - so that the percentages of each combination are stacked on one bar. 我要实现的目标是以某种方式绘制StartPos及其方向,持续时间和百分比(H,M,W; Hit,Miss,Wrong)的图形-以便将每种组合的百分比堆叠在一个条上。 It's kind of difficult for me explain, so I'll just show you what I already tried: 这对我来说很难解释,所以我只向您展示我已经尝试过的内容:

df36= ggplot() +
  geom_bar(data=df, mapping=aes(x=as.factor(StartPos), fill=Duration, 
  y=p_test),stat="identity", position="dodge2") + 
  labs(x="StartPos", y="Hitrate") + ggtitle("Velocity 36°") + theme_bw() +
  scale_fill_gradient(low="red", high="green")


df36

The resulting plot looks like this: 结果图如下:

在此处输入图片说明

It looks a bit crowded and confusing -but I'll get to the point. 看起来有些拥挤和混乱-但我会讲到重点。 The red bars represent the duration of 0.2s, brown 0.5s and green 1.0s. 红色条表示持续时间为0.2s,棕色为0.5s和绿色为1.0s。 The first bar of each color shows the percentage of Hits, the second one percentage of Miss and the last the percentage for Wrongs. 每种颜色的第一个栏显示“命中”百分比,第二个栏显示“未命中”百分比,最后一个栏显示“错误”百分比。 There are also the Startpositions 6 and 8. The three colors on the left of the startPos 6 are Stimuli, that had the direction Left. 还有起始位置6和8。startPos 6左侧的三种颜色是“刺激”,方向为“左”。 The immediate three colors on the right of the StartPos 6 are Stimuli with the direction right - the exact same goes for the StartPos 8. StartPos 6右边的直接三种颜色是Stimuli,方向正确-StartPos 8完全相同。

That is basically the stuff that I need - but it doesn't look good. 这基本上是我需要的东西-但看起来不太好。 The thing that I want to achieve is, "stacking" the percentages of H,M and W - so that there are no "little" bars beside each" Hit percentage and mark them somehow to distinguish the percentage of H from M and so on. If that's not possible I'd like to paint/color the response in different colors and have them shown in a legend . For example ~ M in black and W in yellow. 我要实现的是“堆叠” H,M和W的百分比-以便每个“命中百分比”旁边都没有“小”小节,并对其进行标记,以区分H和M的百分比,依此类推。 。如果不可能,我想用不同的颜色绘制/着色响应,并在图例中显示它们,例如〜M黑色和W黄色。

Is there any way to do this? 有什么办法吗? I'm kinda lost now. 我有点迷路了。 Thanks in advance! 提前致谢!

This isn't exactly what you want (it doesn't use position="dodge2" ), but I think produces an output that is more clear than plotting all that data on a single x-axis. 这并不是您想要的(它不使用position="dodge2" ),但是我认为所产生的输出要比在单个x轴上绘制所有数据更清晰。 This way, the variables are grouped for easier labelling and, of course, interpretation, which should be the main objective of a plot. 这样,将变量分组以便于标记和解释,这应该是绘图的主要目标。 We stack the bars with fill = Response , then use facet_grid to split the groups up. 我们用fill = Response堆叠条形,然后使用facet_grid拆分组。 As an aside, I don't think scale_fill_gradient should be used for categorical variables. scale_fill_gradientscale_fill_gradient ,我不认为scale_fill_gradient应该用于分类变量。

# set facet labels
facet_labels <- as_labeller(c(`Left` = "Direction = Left", `Right` = "Direction = Right", `6` = "StartPos = 6", `8` = "StartPos = 8"))

ggplot(df) + 
    geom_col(aes(x = as.factor(Duration), y = p_test, fill = Response)) + 
    facet_grid(Direction ~ StartPos, switch = "y", labeller = facet_labels) +
    xlab("Duration") +
    ylab("Response proportion")

输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM