使用ggplot绘图,根据条形之间的差异进行着色

3

我已经进行了相当多的搜索,正在尝试做以下事情。我有一个条形图,每个值有两个闪避的条形。每个条形代表一个百分比。(大致如此,因为我还不能发布图片)

Feature |XXXXXXXXXXXXXX  %50
        |XXXXXXXX        %25

我希望的是,当百分比差异大于15%时,将其中一个柱子的颜色更改为“红色”。以下是我使用的数据:
Feature variable          value
A       "Percent Done"    50
B       "Planned"         25
A       "Percent Done"    10
B       "Planned"         80

代码:

p3 <- ggplot(plotdata, aes(x = Feature, y = value, fill = variable))
p3 <- p3 + geom_bar( position ="dodge", stat ="identity")+ 
      coord_flip() + theme_minimal()

基本上,如果我们看一下顶部的“模拟”,因为两个条之间的百分比大于15%,我希望其中一个条是不同的颜色(第三种颜色),如下所示:

enter image description here

我考虑使用ifelse来设置颜色,但是我还没有能够实现它。我的想法是使用ifelse来返回我想要使用的颜色。所以,如果两个条之间的差异> 15,则返回这种颜色,否则返回另一种颜色。有人知道这是否可能吗?


你已经尝试了什么?你想用什么来创建你的图表?基础 R 图形?Ggplot?还是其他东西?你能发布生成条形图的代码,但不要使用红色颜色吗? - Heroka
你能提供一些可重现的代码吗?一种选项是将 aes 设置为 aes(fill = percentage > 15) - drmariod
@drmariod和您代码的'dput',这将非常好。 - SabDeM
抱歉大家!我正在使用Ggplot。我已经更新了帖子并附上了代码。 - abemo
你有考虑过将填充设置为具有自己断点的自定义颜色比例尺吗? - DaveRGP
嗯,在你的情况下可能有点棘手,因为你已经在使用fill来表示不同的东西了,但是你可以使用color=value <= 15来根据百分比改变geom_bar的轮廓。 - drmariod
3个回答

2
您可以在调用ggplot之前创建填充颜色的向量。
## Sample data
dat <- data.frame(`Percent Done`=c(25,10,15),
                  Planned=c(50,80,20),
                  Feature=factor(c("A","B","C"), levels=c("C","B","A")))
library(reshape2)
dat <- melt(dat, id.vars = "Feature")  # reshape the data to long format

## Get the fill colors
dat <- dat[order(dat$Feature, dat$variable, decreasing = T), ]
dat$fills <- ifelse(c(0, abs(diff(dat$value))) > 15, "red", "yellow")
dat$fills[c(T,F)] <- "black"

ggplot(dat, aes(Feature, value, group=variable, order=rev(variable))) +
  geom_histogram(stat="identity", aes(fill=fills), position=position_dodge()) +
  scale_fill_manual(values=c("black","red","yellow"), labels=c("Plan",">15%", "Within 15%")) +
  coord_flip() +
  theme_bw()

输入图像描述

你可能也可以使用 ggplot 调用中的隐藏变量来做到这一点,但这会更棘手。

1
我的回答与@nongkrong的回答相同:
set.seed(1)
x<-data.frame(feat=letters[1:20],plan=runif(20),exec=runif(20)) # create some dummy data
x$col<-((x$plan-x$exec)>=.15)+1 #create a color column
library(reshape2)
y<-melt(x,id.vars = c("feat","col")) # make it long
y$col[which(y$variable=="plan")]<-0 # create a color difference between planed and executed
y$col<-as.factor(y$col) # make it factor, so we can use it in ggplot
ggplot(y,aes(feat,value,fill=col))+geom_bar(position=position_dodge(),stat="identity")+scale_fill_manual(values=c("black","green","red")) # Create a scale fill with the desired colors

1
使用dplyr/tidyr:
data.frame('Feature' = c('A', 'A', 'B', 'B'), 
           'variable' = c("PercentDone", "Planned", "PercentDone", "Planned"),
           "value"=c(35,50,10,80)) %>% 
   spread(variable, value) %>% 
   mutate(colour=ifelse(Planned-PercentDone <= 15, "Within 15%", ">15%")) %>% 
   gather("variable", "value", 2:3) %>% 
   mutate(colour = ifelse(variable == "Planned", "Plan", colour)) %>%
   ggplot(aes(x=Feature, y=value, fill=relevel(factor(colour), ref="Plan"))) +
   geom_bar(stat="identity", position="dodge")

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接