ggplot2堆积的条形图,其中每个值都是一个组/颜色 [英] ggplot2 stacked bar graph in which every value is a group/color

查看:206
本文介绍了ggplot2堆积的条形图,其中每个值都是一个组/颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,我是新手,我很抱歉,如果我的帖子不干净,或者我没有找到足够的答案。



我试图用ggplot2绘制一个堆积的条形图并且几乎在那里。这是我到目前为止。我有一个数据集有2列(长度和计数)。我已将它附加到此帖子。我读过它并将其绘制为这样:

 示例<  -  read.delim(example.txt,sep, )
ggplot(data = example,aes(x = Length,y = Count,fill = Count))+ geom_bar(stat =identity)

这给我的图是Fig1A



然而,我正在寻找的是, Count列被赋予它自己的颜色。它应该看起来像图1B(Excel示例):我试图按照我将它绘制在Excel中的方式绘制它(图1C),但不能复制这种类型的用ggplot2绘图,我无法让我的data.frame适合这种布局。






ggplot自动选择渐变色标,但您可以使用一个不同的调色板或每个因素的手动颜色列表。例如,您可以通过调用以下选项为数据集中的每个级别选择随机颜色:

 颜色<  -  sample(colors( ),长度(水平(因子(示例$ Count))))

  + scale_fill_manual(values = colors)

给出类似于此的内容:
< img src =https://i.stack.imgur.com/jhBm2.pngalt =randcolor>

或者,您可以选择遵循R的内置颜色代码。尝试运行

 颜色()

,您将获得可用颜色的完整列表。您可以根据需要手动挑选,也可以选择前n个元素,其中n等于您的级别数量。

 颜色<  -  colors()[seq(1,length(levels(factor(example $ Count))),by = 1)] 

并遵循上述步骤。这一贯给这张图:



< img src =https://i.stack.imgur.com/DHti8.pngalt =fixed>


this is my first post and I am new to coding, I apologise if my post is not clean or I havent looked for answers enough.

I am trying to plot a stacked bar graph with ggplot2 and am almost there. Here is what I have so far. I have a data set with 2 columns (Length and Count). I have attached it to this post. I read it and plot it like this:

example <- read.delim("example.txt", sep",")
ggplot(data=example, aes(x=Length, y=Count, fill=Count))+geom_bar(stat="identity")

The graph this gives me is Fig1A

What I am looking for, however, is that every value in the column "Count" is given its own color. It should look like Fig1B (Excel example):

I tried to plot it the way I would plot it in Excel (Fig1C) but was not able to replicate this kind of plot with ggplot2, I wasnt able to have my data.frame fit this kind of layout.

Example file (example.txt

Thank you

解决方案

Try using the column count as a factor, rather than the column itsself.

ggplot(data=example, aes(x=Length, y=Count, fill=factor(Count)))+geom_bar(stat="identity")

returns

ggplot automatically chooses a gradient color scale, but you can change it with a different color palette or a manual list of colors for each factor. For example, you can choose a random color for each level in your dataset by calling:

colors <- sample(colours(),length(levels(factor(example$Count))))

and adding this to your plot

+ scale_fill_manual(values = colors) 

which gives something similar to this:

alternatively, you can choose to follow R's built in color codes. Try running

colours()

and you will get a full list of available colors. You can hand pick as many as you need or you can select the first n elements, with n equal to your amount of levels

colors <- colours()[seq(1,length(levels(factor(example$Count))),by=1)]

and following the above procedure. That consistently gives this graph:

这篇关于ggplot2堆积的条形图,其中每个值都是一个组/颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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