在ggplot2中更改颜色填充顺序 [英] In ggplot2 change colour fill order

查看:142
本文介绍了在ggplot2中更改颜色填充顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ggplot2中手动设置直方图条的顺序,因此它们不只是使用类似以下内容的字母顺序从左到右列出:

I am manually setting the order of my histogram bars in ggplot2, so they don't just list in alphabetical order from left to right using something like:

p + xlim("second", "first", "third")

然后当我设置颜色时

p + scale_fill_grey()

条形按字母顺序从最深到最浅进行着色.我可以让他们填写从第二"到第一"到第三"的顺序吗?甚至将不同的条形组合为相同的颜色?

the bars are colored in alphabetical order darkest to lightest. Can I get them to fill in order from "second" to "first" to "third"? Or even group different bars as the same color?

推荐答案

我不确定您要问什么;我很困惑.但是,如果使用 scale_fill_grey(),则x轴上首先出现的内容将变暗.也就是说,即使您对轴的因子水平进行重新排序,您最终也会看到第一个条形图具有深色.只要我从您的问题中看到,您可能想要手动为条形分配颜色.希望这能使您前进.

I am not entire sure what you are asking; I am confused. But, if you use scale_fill_grey(), whatever comes first on the x-axis will be dark. That is, even when you reorder the levels of your factor for the axis, you will end up seeing the first bar plot has dark colour. As long as I see from your question, you may want to manually assign colours to bars. Hope this will let you move forward.

# I create a sample data, which you should provide when you ask a question.
location <- c("London", "Paris", "Madrid")
value <- runif(3, 15,30)

foo <- data.frame(location, value, stringsAsFactors = F)
foo$location <- factor(foo$location)

# Colors are assigned in the alphabetical order of the cities.

ggplot(foo, aes(x = location, y = value, fill = location)) +
    geom_bar(stat="identity") +
    scale_fill_manual(values=c("black", "grey", "white"))

现在,我更改城市的顺序并绘制另一个数字.但是,颜色仍然按字母顺序分配.

Now I change the order of the cities and draw another figure. But, the colors are still assigned in the alphabetical order.

foo$location <- factor(foo$location, levels = c("Paris", "Madrid", "London"))

ggplot(foo, aes(x = location, y = value, fill = location)) +
    geom_bar(stat="identity") +
    scale_fill_manual(values=c("white", "grey", "black"))

这篇关于在ggplot2中更改颜色填充顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆