带有GGPlot的一个图上的条形图和折线图 [英] Bar Chart + Line Graph on One Plot with GGPlot

查看:135
本文介绍了带有GGPlot的一个图上的条形图和折线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作分组的条形图...进行一些修改。

I am attempting to make a grouped bar chart...with a few modifications.

我的图表有四个列:季度,人,百分比,总百分比。我的数据示例如下:

I have four "columns" for my chart: Quarter, Person, Percentage, Total Percentage. An example of my data looks like this:

|人季度|百分比总百分比|

| Person | Quarter | Percentage | Total Percentage |

| -------- | --------- | ------------ | ------------------ |

|-------- |--------- |------------ |------------------ |

| A | 1 | 40%| 50%|

| A | 1 | 40% | 50% |

| A | 2 | 45%| 50%|

| A | 2 | 45% | 50% |

| A | 3 | 55%| 50%|

| A | 3 | 55% | 50% |

| A | 4 | 60%| 50%|

| A | 4 | 60% | 50% |

| B | 1 | 35%| 42%|

| B | 1 | 35% | 42% |

,依此类推。

我已成功使用以下代码制作了条形图:

I have successfully made my bar chart using this code:

ggplot(data=point3, aes(x=Person, y=Percentage, fill=Quarter)) + 
+     geom_bar(colour="black", stat="identity", position=position_dodge(), size=.3)                       
+     scale_fill_hue(name="Quarter") 
+     xlab(" ") + ylab("Percentage") 
+     theme_bw()

但是,我想更改一些内容。首先,我想间隔开x轴。每个人有四个酒吧,我想知道如何做到这一点,以便每个人之间都有更多的空间。我遇到了麻烦,因为X轴不包含任何种类的任何数字。有人知道怎么做吗?

However, I would like to change a few things. First, I would like to space out the x-axis. There are four bars per person and I was wondering how I could make it so that there is more space in between each person. I've run into trouble with this because the X axis doesn't contain any numbers of any sort. Does anyone know how to do this?

第二,我想为每个人添加一条代表他们总百分比的垂直线。这样,观看者可以很容易地看到该人在其年平均水平上所表现的哪个季度,以及他们表现出色的那个季度。我在使用

Second, I would like to add a vertical line for each person representing their total percentage. This way, it would be easy for the viewer to see which quarter the person performed under their yearly average and which quarter they over-performed. I have had a little success with this using

geom_point(data=point3, aes(x=Person, y=Total Percentage))

在上一个代码的末尾,但这只是在第二个和第三个小节之间添加了一个点(中间)。我通过Google分组的条形图对进行了模拟。这个只有3个酒吧,但也许您可以理解。我这次遇到的一些问题是,尽管它们的范围相同,但条形图的y轴是性能,而折线的y轴是总体性能。 R似乎不喜欢这样。同样,整个x轴也不是数字。

at the end of the previous code, but this simply adds a dot between the second and third bars (the middle). I made a mock up of this using a grouped bar chart I found via Google. This one only has 3 bars, but perhaps you can get the idea. Some of the problems I ran into this time were, although they are on the same scale, the y-axis for the bar charts is "Performance" while the y-axis for the line is "Total Performance". R did not seem to like this. Also, the whole x-axis not being numeric.

我使用ggplot2是因为我不知道还有什么用,但是如果您知道要使用的任何技术/软件包,使这项工作,我很乐意改变这一点。我也知道我可能需要重塑数据,这也很酷。

I used ggplot2 because I didn't know what else to use, but if you know of any technique/package to make this work, I am happy to change that. I am also aware I may need to reshape my data, that is cool, too.

非常感谢。

推荐答案

我必须添加到您的样本数据以使其更完整并使其具有R友好列名称。

I had to add to your sample data to make it more "complete" and make R friendly column names.

point3<-structure(list(Person = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), 
.Label = c("A", "B"), class = "factor"), 
Quarter = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), Percentage = c(40L, 
45L, 55L, 60L, 35L, 40L, 60L, 25L), TotalPercentage = c(50L, 50L, 50L, 50L, 
42L, 42L, 42L, 42L)), .Names = c("Person", "Quarter", "Percentage", 
"TotalPercentage"), class = "data.frame", row.names = c(NA, -8L))

然后您就可以控制 width = position_dodge(width =)的间距。然后添加条形图,将经过简化的数据框传递到 geom_segment 并将因子转换为数字,以便能够沿x轴延伸。

Then you can control the spacing with width= and position_dodge(width=). Then to add the bar, I pass a reduced data frame to geom_segment and convert the factors to numbers to be able to stretch it out across the x-axis.

ggplot(data=point3, aes(x=Person, y=Percentage, fill=factor(Quarter))) + 
    geom_bar(colour="black", stat="identity", 
        position=position_dodge(width=.80), width=.5) +
    geom_segment(data=unique(point3[,c("Person","TotalPercentage")]), 
         aes(x=as.numeric(Person)-.4, xend=as.numeric(Person)+.4, 
         y=TotalPercentage, yend=TotalPercentage),
        inherit.aes=F, size=1.2) +
    scale_fill_hue(name="Quarter") +
    xlab(" ") + ylab("Percentage") +
    theme_bw()

最终看起来像

这篇关于带有GGPlot的一个图上的条形图和折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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