显示更改%的系列的R ggplot

时间:2018-02-01 20:46:54

标签: r ggplot2

我有这个条形图,并且想着我如何能够捕捉那些让我们改变的对象> 20%,或最佳解决方案将在情节上显示该百分比,如在模拟图片上。我感觉R包可以做任何事情......

library(tidyverse)

df1 <-  data.frame(yy=2017,  F1=11, F2=11, F3=16)
df2 <-  data.frame(yy=2018,  F1=13, F2=33, F3=22)
df <- rbind(df1,df2)

df %>% 
  gather(type,value,-yy) %>%           # reshape data
  mutate(yy = factor(yy)) %>%          # update variable to a factor
  ggplot(aes(type, value, fill=yy)) +
  geom_bar(stat = "identity", position = "dodge")

enter image description here

2 个答案:

答案 0 :(得分:4)

你的意思是这样吗?

library(tidyr);
library(dplyr);
library(ggplot2);


df %>%
  gather(type, value, -yy) %>%           # reshape data
  mutate(yy = factor(yy)) %>%          # update variable to a factor
  ggplot(aes(type, value, fill = yy, text = yy)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(
      aes(y = value, label = scales::percent(value / sum(value))), 
      vjust = -0.5, 
      position = position_dodge(width = 1))

enter image description here

或者,如果您所追求的是{em>更改每个type(按照@neilfs的建议),您可以这样做:

df %>%
  gather(type, value, -yy) %>%
  mutate(yy = factor(yy)) %>%
  group_by(type) %>%
  mutate(change = (value - lag(value)) / lag(value)) %>%
  mutate(change = if_else(is.na(change), 0, change)) %>%
  ggplot(aes(type, value, fill = yy, text = yy)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(
      aes(y = value, label = scales::percent(change)),
      vjust = -0.5,
      position = position_dodge(width = 1))

enter image description here

或者如果你不想要&#34; 0%&#34;标签,

percent_format <- function(x, nplaces = 2) {
    x <- plyr::round_any(x, 10 ^ (-(nplaces + 2)));
    s <- rep("", length(x));
    s[x > 0] <- paste0(scales::comma(x[x > 0] * 100), "%");
    return(s);
}

df %>%
  gather(type, value, -yy) %>%
  mutate(yy = factor(yy)) %>%
  group_by(type) %>%
  mutate(change = (value - lag(value)) / lag(value)) %>%
  mutate(change = if_else(is.na(change), 0, change)) %>%
  ggplot(aes(type, value, fill = yy, text = yy)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(
      aes(y = value, label = percent_format(change, 2)),
      vjust = -0.5,
      position = position_dodge(width = 1))

enter image description here

最后,如果您希望标签居中,只需删除position = position_dodge(width = 1)

样本数据

df1 <-  data.frame(yy=2017,  F1=11, F2=11, F3=16)
df2 <-  data.frame(yy=2018,  F1=13, F2=33, F3=22)
df <- rbind(df1,df2)

答案 1 :(得分:0)

可能吗

library(tidyverse)

df1 <-  data.frame(yy=2017,  F1=11, F2=11, F3=16)
df2 <-  data.frame(yy=2018,  F1=13, F2=33, F3=22)
df <- rbind(df1,df2)


perc <- (df[2, 2:4] - df[1, 2:4]) / df[1, 2:4] * 100
perc <- paste0(round(perc, 2), "%")

df_tidy <- df %>% 
  gather(type,value,-yy) %>%           # reshape data
  mutate(yy = factor(yy))             # update variable to a factor

ggplot() +
  geom_bar(data = df_tidy, aes(type, value, fill=yy), stat = "identity", position = "dodge") + 
  geom_text(aes(x = 1:length(unique(df_tidy$type)), y = apply(df[, 2:4], 2, max) + 1, label = perc))

这给了

enter image description here