tsdryt - 11 months ago 70
R Question

# Bar Chart + Line Graph on One Plot with 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 | 2 | 45% | 50% |

| A | 3 | 55% | 50% |

| A | 4 | 60% | 50% |

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

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.

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.

Thanks so much.

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

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

That ends up looking like