Bar Chart + Line Graph on One Plot with GGPlot

时间:2021-10-07 23:48:22

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%|

and so forth for.

等等。

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()

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?

但是,我想改变一些事情。首先,我想把x轴分开。每个人有四个酒吧,我想知道如何做到这一点,以便每个人之间有更多的空间。我遇到了麻烦,因为X轴不包含任何数量的任何数字。有谁知道如何做到这一点?

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))

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.

在前一个代码的末尾,但这只是在第二个和第三个条形图(中间)之间添加一个点。我使用通过Google找到的分组条形图模拟了这一点。这个只有3个酒吧,但也许你可以得到这个想法。我遇到的一些问题是,尽管它们的尺寸相同,但条形图的y轴是“性能”,而线的y轴是“总性能”。 R似乎不喜欢这个。此外,整个x轴不是数字。

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.

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

Thanks so much.

非常感谢。

2 个解决方案

#1


0  

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

我必须添加到您的示例数据中以使其更“完整”并使R友好列名称。

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))

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.

然后你可以用width =和position_dodge(width =)来控制间距。然后,为了添加条形,我将缩小的数据框传递给geom_segment,并将因子转换为数字,以便能够在x轴上拉伸它。

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()

That ends up looking like

最终看起来像

Bar Chart + Line Graph on One Plot with GGPlot

#2


1  

Another way to do it is using geom_errorbar:

另一种方法是使用geom_errorbar:

ggplot(data=point3, aes(x=Person, y=Percentage, fill=factor(Quarter))) +
  geom_bar(colour="black", stat="identity", position=position_dodge(width=0.9), width=.8)  +
  geom_errorbar(aes(y=TotalPercentage, ymax=TotalPercentage, ymin=TotalPercentage), linetype="solid",size=1) +
  scale_fill_hue(name="Quarter") +
  xlab(" ") + ylab("Percentage") +
  theme_bw()

Bar Chart + Line Graph on One Plot with GGPlot

#1


0  

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

我必须添加到您的示例数据中以使其更“完整”并使R友好列名称。

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))

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.

然后你可以用width =和position_dodge(width =)来控制间距。然后,为了添加条形,我将缩小的数据框传递给geom_segment,并将因子转换为数字,以便能够在x轴上拉伸它。

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()

That ends up looking like

最终看起来像

Bar Chart + Line Graph on One Plot with GGPlot

#2


1  

Another way to do it is using geom_errorbar:

另一种方法是使用geom_errorbar:

ggplot(data=point3, aes(x=Person, y=Percentage, fill=factor(Quarter))) +
  geom_bar(colour="black", stat="identity", position=position_dodge(width=0.9), width=.8)  +
  geom_errorbar(aes(y=TotalPercentage, ymax=TotalPercentage, ymin=TotalPercentage), linetype="solid",size=1) +
  scale_fill_hue(name="Quarter") +
  xlab(" ") + ylab("Percentage") +
  theme_bw()

Bar Chart + Line Graph on One Plot with GGPlot