用绝对值和百分比值+标签绘制成组的条形图

时间:2019-02-24 17:21:36

标签: r ggplot2

我对R特别是ggplot非常陌生。对于下一个结果,我认为我需要在需要您帮助的情况下从plot()更改为ggplot()

我有一个带有数值的数据框。一栏是绝对数,另一栏是所属百分比值。我有3个“两组”指标a,b和c。 行名是6个观察值,并存储在第一列“ X”中。 我想将它们绘制成一种分组的条形图,其中3个指标的absolute + percent列彼此相邻。

示例数据框:

df = data.frame(X = c("e 1","e 1,5","e 2","e 2,5","e 3","e 3,5","e 4"),
            a_abs=c(-0.3693,-0.0735,-0.019,0.0015,0,-0.0224,-0.0135),
            a_per=c(-0.4736,-0.0943,-0.0244,0.0019,0,-0.0287,-0.0173),
            b_abs=c(-0.384,-0.0733,-0.0173,0.0034,0,-0.0204,-0.0179),
            b_per=c(-0.546,-0.1042,-0.0246,0.0048,0,-0.029,-0.0255),
            c_abs=c(-0.3876,-0.0738,-0.019,0.0015,0,-0.0225,-0.0137),
            c_per=c(-0.4971,-0.0946,-0.0244,0.0019,0,-0.0289,-0.0176))

由于@jonspring,我通过使用以下代码获得了以下情节:

df3 <- df %>%
  gather(column, value, -X) %>%
  mutate(group = str_sub(column, end   = 2),
         stat  = str_sub(column, start = 4)) %>%
  select(-column) %>%
  spread(stat, value) %>%
  mutate(combo_label = paste(sep="\n",
                             scales::comma(abs, accuracy = 0.001),
                             scales::percent(per, accuracy = 0.01)))
df3$group = gsub(df3$group,pattern = "CK",replacement = "Cohen's\nKappa")
df3$group = gsub(df3$group,pattern = "JA",replacement = "Jaccard")
df3$group = gsub(df3$group,pattern = "KA",replacement = "Krippen-\ndorff's Alpha")

crg = ifelse(df3$abs< 0,"red","darkgreen")
ggplot(df3, aes(group, abs, label = combo_label)) +
  geom_segment(aes(xend = group,
                   yend = 0),
                   color = crg) +
  geom_point() +
  geom_text(vjust = 1.5,
            size = 3,
            lineheight = 1.2) +
  scale_y_continuous(expand = c(0.2,0)) +
  facet_grid(~X) +
  labs(x= "Exponent", y = "Wert")

plot output

当我缩放并看到正值时,标签将写在线段内。如何根据正值或负值将它们放在上方/下方?

放大coord_cartesian(ylim = c(-0.015,0.005))

zoomed plot

感谢您的帮助。

编辑:我已经找到了解决方案。就像颜色从红色变为绿色一样,我在ifelse参数中使用了vjust

1 个答案:

答案 0 :(得分:0)

使用ggplot显示此类数据的方式有很多种。我强烈建议您查看https://r4ds.had.co.nz/data-visualisation.html(如果尚未注册)。

您会发现有一个建议,如果您首先将数据转换为长格式(又称“整洁”),ggplot几乎总是可以更好地工作。这会将数据的每个维度放入其自己的列中,以便您可以将维度映射到视觉美感。这是一种方法:

library(tidyverse)  
df2 <- df %>%
  gather(column, value, -X) %>%
  mutate(group = str_sub(column, end   = 1),
         stat  = str_sub(column, start = 3),
         value_label = if_else(stat == "per", 
                               scales::percent(value, accuracy = 0.1),
                               scales::comma(value, accuracy = 0.01)))

现在,组a/b/c和数据类型abs/per都在其自己的列中,所有值都集中在一个列中,并且我们还有适合该类型的文本标签数据。

> head(df2)
      X column   value group stat value_label
1   e 1  a_abs -0.3693     a  abs       -0.37
2 e 1,5  a_abs -0.0735     a  abs       -0.07
3   e 2  a_abs -0.0190     a  abs       -0.02
4 e 2,5  a_abs  0.0015     a  abs        0.00
5   e 3  a_abs  0.0000     a  abs        0.00
6 e 3,5  a_abs -0.0224     a  abs       -0.02

通过这种方式,更容易尝试使用ggplot选项的不同组合,这可以帮助突出显示数据中的不同比较。

例如,如果要比较每个组中的不同观察值,可以将每个组放入一个构面中,并将每个观察值沿x轴放置:

ggplot(df2, aes(X, value, label = value_label)) +
  geom_segment(aes(xend = X, yend = 0), color = "blue") +
  geom_point() +
  geom_text(vjust = 2, size = 2) +
  facet_grid(stat~group)

enter image description here

或者,如果您想突出显示每个观察值中不同组的比较方式,可以交换它们,如下所示:

ggplot(df2, aes(group, value, label = value_label)) +
  geom_segment(aes(xend = group, yend = 0), color = "blue") +
  geom_point() +
  geom_text(vjust = 2, size = 2) +
  facet_grid(stat~X)

enter image description here

您还可以尝试合并absper数据,因为它们仅基于适用于每个组和/或观察值的不同分母而略有不同。为此,转换数据以将每个absper保持在一起可能更简单:

df3 <- df %>%
  gather(column, value, -X) %>%
  mutate(group = str_sub(column, end   = 1),
         stat  = str_sub(column, start = 3)) %>%
  select(-column) %>%
  spread(stat, value) %>%
  mutate(combo_label = paste(sep="\n",
                             scales::comma(abs, accuracy = 0.01),
                             scales::percent(per, accuracy = 0.1)))

ggplot(df3, aes(group, abs, label = combo_label)) +
  geom_segment(aes(xend = group, yend = 0), color = "blue") +
  geom_point() +
  geom_text(vjust = 1.5, size = 2, lineheight = 0.8) +
  scale_y_continuous(expand = c(0.2,0)) +
  facet_grid(~X)

enter image description here