改变ggplot中一个条的颜色 [英] change color of only one bar in ggplot

查看:120
本文介绍了改变ggplot中一个条的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ggplot中只着色一个栏。这是我的资料框架:

  area<  -  c(Północ,Południe,Wschód,Zachód )
sale< - c(16.5,13.5,14,13)
df.sale< - data.frame(area,sale)
colnames(df.sale) - c(Obszarsprzedaży,Liczba sprzedanychproduktów(w tys。))

  plot.sale.bad<  -  ggplot(data = df.sale,aes(x = area,y = sales,fill = area))+ 
geom_bar(stat =identity)+
scale_fill_manual(values = c(black,red,black,black))+
xlab(colnames(df.sale)[1])+
ylab(colnames(df.sale)[2])+
ggtitle(Porównaniesprzedaży)

我想只有一个彩色的条和其他3个有默认颜色(darkgrey,不是黑色,我)。

解决方案

p>选项1:只更改一个条的颜色。根据Henrick的建议,你可以创建一个新的变量NAs默认颜色和字符串/因素的非默认颜色(第一个恰好是红色):

  area.color<  -  c(NA,withcolor,NA,NA)
plot.sale.bad< - ggplot(data = df.sale,aes x = area,y = sale,fill = area.color))+
geom_bar(stat =identity)+
xlab(colnames(df.sale)[1])+
ylab(colnames(df.sale)[2])+
ggtitle(Porównaniesprzedaży)
plot.sale.bad

选项2:找到您喜欢的默认深灰色的名称。这不是默认颜色,如果你简单地删除 scale_fill_manual 行在这种情况下,你得到四个不同的粉彩),所以我假设你的意思是灰色由 area.color == NA 的那些条块生成的代码块。在这种情况下,您可以查看 scale_fill_discrete 的源代码(或args):

 > args(scale_fill_discrete)
#function(...,h = c(0,360)+ 15,c = 100,l = 65,h.start = 0,
#direction = 1,na .value =grey50)
#NULL

na.value grey50。所以如果你想使用 scale_fill_manual ,你可以这样做:

  plot.sale.bad<  -  ggplot(data = df.sale,aes(x = area,y = sale,fill = area))+ 
geom_bar(stat =identity)+
scale_fill_manual(values = c(grey50,red,grey50,grey50))+
xlab(colnames(df.sale)[1])+
ylab df.sale)[2])+
ggtitle(Porównaniesprzedaży)
plot.sale.bad


I want to color only one bar in ggplot. This is my data frame:

area <- c("Północ", "Południe", "Wschód", "Zachód")
sale <- c(16.5, 13.5, 14, 13)
df.sale <- data.frame(area, sale)
colnames(df.sale) <- c("Obszar sprzedaży", "Liczba sprzedanych produktów (w tys.)")

And code for plotting:

plot.sale.bad <- ggplot(data=df.sale, aes(x=area, y=sale, fill=area)) +
  geom_bar(stat="identity") +
  scale_fill_manual(values=c("black", "red", "black", "black")) +
  xlab(colnames(df.sale)[1]) +
  ylab(colnames(df.sale)[2]) +
  ggtitle("Porównanie sprzedaży") 

I would like to have only one bar colored and 3 others to have default color (darkgrey, not black, it looks bad for me). How can I change color of only on bar or how to get name of the default color of bars to put them instead of black?

解决方案

Option 1: Change color of only one bar. Following Henrick's suggestion, you can create a new variable with NAs for the default color and character strings/factors for non-default colors (the first one happens to be red):

area.color <- c(NA, "withcolor", NA, NA)
plot.sale.bad <- ggplot(data=df.sale, aes(x=area, y=sale, fill=area.color)) +
  geom_bar(stat="identity") +
  xlab(colnames(df.sale)[1]) +
  ylab(colnames(df.sale)[2]) +
  ggtitle("Porównanie sprzedaży") 
plot.sale.bad

Option 2: Find the name of the default dark gray color you like. This is not the default color if you simply remove the scale_fill_manual line in your original code (in that case, you get four different pastels), so I assume you mean the grey color produced by the code chunk just above this paragraph, for those bars where area.color==NA. In that case, you might look at the source code (or args, anyway) for scale_fill_discrete:

> args(scale_fill_discrete)
# function (..., h = c(0, 360) + 15, c = 100, l = 65, h.start = 0, 
#     direction = 1, na.value = "grey50") 
# NULL

The default for na.value is "grey50". So if you wanted to use scale_fill_manual, you could do it like so:

plot.sale.bad <- ggplot(data=df.sale, aes(x=area, y=sale, fill=area)) +
  geom_bar(stat="identity") +
  scale_fill_manual(values=c("grey50", "red", "grey50", "grey50")) +
  xlab(colnames(df.sale)[1]) +
  ylab(colnames(df.sale)[2]) +
  ggtitle("Porównanie sprzedaży") 
plot.sale.bad

这篇关于改变ggplot中一个条的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆