Pyramid plot in R - r

For an example dataset, I create a pyramid plot by country showing levels (%) of overweight males and females in a population.
library(plotrix)
xy.males.overweight<-c(23.2,33.5,43.6,33.6,43.5,43.5,43.9,33.7,53.9,43.5,43.2,42.8,22.2,51.8,
41.5,31.3,60.7,50.4)
xx.females.overweight<-c(13.2,9.4,13.5,13.5,13.5,23.7,8,3.18,3.9,3.16,23.2,22.5,22,12.7,12.5,
12.3,10,0.8)
agelabels<-c("uk","scotland","france","ireland","germany","sweden","norway",
"iceland","portugal","austria","switzerland","australia","new zealand","dubai","south africa",
"finland","italy","morocco")
par(mar=pyramid.plot(xy.males.overweight,xx.females.overweight,labels=agelabels,
gap=9))
I found this approach using 'plotrix' here:
https://stats.stackexchange.com/questions/2455/how-to-make-age-pyramid-like-plot-in-r
I wish to create a slightly more detailed pyramid plot, with the addition of a stacked bar chart on both sides showing overweight AND percentage obese for males and females (preferably in different shades of red/blue). Example data values for 'obese' are listed below:
xx.females.obese<-c(23.2,33.5,43.6,33.6,43.5,23.5,33.9,33.7,23.9,43.5,18.2,22.8,22.2,31.8,
25.5,25.3,31.7,28.4)
xy.males.obese<-c(13.2,9.4,13.5,13.5,13.5,23.7,8,3.18,3.9,3.16,23.2,22.5,22,12.7,12.5,
12.3,10,0.8)
Also, if 'Age' on the graph could be changed (to country), that would be helpful to.
Many thanks in advance for any help/advice. I am open to using plotrix or ggplot2 as appropriate.

Plotrix might be easier, but it is possible to disassemble ggplot charts, and arrange them as a pyramid plot. Using #eipi10's data (thanks), and adapting code from drawing-pyramid-plot-using-r-and-ggplot2, I draw separate plots for "males", "females", and the "country" labels. Also, I grab a legend from one of the plots. The trick is to get the tick marks for the left chart to appear on the right side of the chart - I adapted code from mirroring-axis-ticks-in-ggplot2. The four bits (the "female" plot, the country labels, the "male plot", and the legend) are put together using gtable functions.
Minor edit: Updating to ggplot2 2.2.1
# Packages
library(plyr)
library(ggplot2)
library(scales)
library(gtable)
library(stringr)
library(grid)
# Data
mov <-c(23.2,33.5,43.6,33.6,43.5,43.5,43.9,33.7,53.9,43.5,43.2,42.8,22.2,51.8,
41.5,31.3,60.7,50.4)
fov<-c(13.2,9.4,13.5,13.5,13.5,23.7,8,3.18,3.9,3.16,23.2,22.5,22,12.7,12.5,
12.3,10,0.8)
fob<-c(23.2,33.5,43.6,33.6,43.5,23.5,33.9,33.7,23.9,43.5,18.2,22.8,22.2,31.8,
25.5,25.3,31.7,28.4)
mob<-c(13.2,9.4,13.5,13.5,13.5,23.7,8,3.18,3.9,3.16,23.2,22.5,22,12.7,12.5,
12.3,10,0.8)
labs<-c("uk","scotland","france","ireland","germany","sweden","norway",
"iceland","portugal","austria","switzerland","australia",
"new zealand","dubai","south africa",
"finland","italy","morocco")
df = data.frame(labs=rep(labs,4), values=c(mov, mob, fov, fob),
sex=rep(c("Male", "Female"), each=2*length(fov)),
bmi = rep(rep(c("Overweight", "Obese"), each=length(fov)),2))
# Order countries by overall percent overweight/obese
labs.order = ddply(df, .(labs), summarise, sum=sum(values))
labs.order = labs.order$labs[order(labs.order$sum)]
df$labs = factor(df$labs, levels=labs.order)
# Common theme
theme = theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
plot.title = element_text(size = 10, hjust = 0.5))
#### 1. "male" plot - to appear on the right
ggM <- ggplot(data = subset(df, sex == 'Male'), aes(x=labs)) +
geom_bar(aes(y = values/100, fill = bmi), stat = "identity") +
scale_y_continuous('', labels = percent, limits = c(0, 1), expand = c(0,0)) +
labs(x = NULL) +
ggtitle("Male") +
coord_flip() + theme +
theme(plot.margin= unit(c(1, 0, 0, 0), "lines"))
# get ggplot grob
gtM <- ggplotGrob(ggM)
#### 4. Get the legend
leg = gtM$grobs[[which(gtM$layout$name == "guide-box")]]
#### 1. back to "male" plot - to appear on the right
# remove legend
legPos = gtM$layout$l[grepl("guide", gtM$layout$name)] # legend's position
gtM = gtM[, -c(legPos-1,legPos)]
#### 2. "female" plot - to appear on the left -
# reverse the 'Percent' axis using trans = "reverse"
ggF <- ggplot(data = subset(df, sex == 'Female'), aes(x=labs)) +
geom_bar(aes(y = values/100, fill = bmi), stat = "identity") +
scale_y_continuous('', labels = percent, trans = 'reverse',
limits = c(1, 0), expand = c(0,0)) +
labs(x = NULL) +
ggtitle("Female") +
coord_flip() + theme +
theme(plot.margin= unit(c(1, 0, 0, 1), "lines"))
# get ggplot grob
gtF <- ggplotGrob(ggF)
# remove legend
gtF = gtF[, -c(legPos-1,legPos)]
## Swap the tick marks to the right side of the plot panel
# Get the row number of the left axis in the layout
rn <- which(gtF$layout$name == "axis-l")
# Extract the axis (tick marks and axis text)
axis.grob <- gtF$grobs[[rn]]
axisl <- axis.grob$children[[2]] # Two children - get the second
# axisl # Note: two grobs - text and tick marks
# Get the tick marks - NOTE: tick marks are second
yaxis = axisl$grobs[[2]]
yaxis$x = yaxis$x - unit(1, "npc") + unit(2.75, "pt") # Reverse them
# Add them to the right side of the panel
# Add a column to the gtable
panelPos = gtF$layout[grepl("panel", gtF$layout$name), c('t','l')]
gtF <- gtable_add_cols(gtF, gtF$widths[3], panelPos$l)
# Add the grob
gtF <- gtable_add_grob(gtF, yaxis, t = panelPos$t, l = panelPos$l+1)
# Remove original left axis
gtF = gtF[, -c(2,3)]
#### 3. country labels - create a plot using geom_text - to appear down the middle
fontsize = 3
ggC <- ggplot(data = subset(df, sex == 'Male'), aes(x=labs)) +
geom_bar(stat = "identity", aes(y = 0)) +
geom_text(aes(y = 0, label = labs), size = fontsize) +
ggtitle("Country") +
coord_flip() + theme_bw() + theme +
theme(panel.border = element_rect(colour = NA))
# get ggplot grob
gtC <- ggplotGrob(ggC)
# Get the title
Title = gtC$grobs[[which(gtC$layout$name == "title")]]
# Get the plot panel
gtC = gtC$grobs[[which(gtC$layout$name == "panel")]]
#### Arrange the components
## First, combine "female" and "male" plots
gt = cbind(gtF, gtM, size = "first")
## Second, add the labels (gtC) down the middle
# add column to gtable
maxlab = labs[which(str_length(labs) == max(str_length(labs)))]
gt = gtable_add_cols(gt, sum(unit(1, "grobwidth", textGrob(maxlab, gp = gpar(fontsize = fontsize*72.27/25.4))), unit(5, "mm")),
pos = length(gtF$widths))
# add the grob
gt = gtable_add_grob(gt, gtC, t = panelPos$t, l = length(gtF$widths) + 1)
# add the title; ie the label 'country'
titlePos = gtF$layout$l[which(gtF$layout$name == "title")]
gt = gtable_add_grob(gt, Title, t = titlePos, l = length(gtF$widths) + 1)
## Third, add the legend to the right
gt = gtable_add_cols(gt, sum(leg$width), -1)
gt = gtable_add_grob(gt, leg, t = panelPos$t, l = length(gt$widths))
# draw the plot
grid.newpage()
grid.draw(gt)

Using ggplot2 and adapting code from this SO answer:
library(plyr)
library(ggplot2)
# Data
mov <-c(23.2,33.5,43.6,33.6,43.5,43.5,43.9,33.7,53.9,43.5,43.2,42.8,22.2,51.8,
41.5,31.3,60.7,50.4)
fov<-c(13.2,9.4,13.5,13.5,13.5,23.7,8,3.18,3.9,3.16,23.2,22.5,22,12.7,12.5,
12.3,10,0.8)
fob<-c(23.2,33.5,43.6,33.6,43.5,23.5,33.9,33.7,23.9,43.5,18.2,22.8,22.2,31.8,
25.5,25.3,31.7,28.4)
mob<-c(13.2,9.4,13.5,13.5,13.5,23.7,8,3.18,3.9,3.16,23.2,22.5,22,12.7,12.5,
12.3,10,0.8)
labs<-c("uk","scotland","france","ireland","germany","sweden","norway",
"iceland","portugal","austria","switzerland","australia",
"new zealand","dubai","south africa",
"finland","italy","morocco")
df = data.frame(labs=rep(labs,4), values=c(mov, mob, fov, fob),
sex=rep(c("Male", "Female"), each=2*length(fov)),
bmi = rep(rep(c("Overweight", "Obese"), each=length(fov)),2))
# Order countries by overall percent overweight/obese
labs.order = ddply(df, .(labs), summarise, sum=sum(values))
labs.order = labs.order$labs[order(labs.order$sum)]
df$labs = factor(df$labs, levels=labs.order)
Plot separate subsets of Male and Female to get a pyramid plot:
ggplot(df, aes(x=labs)) +
geom_bar(data=df[df$sex=="Male",], aes(y=values, fill=bmi), stat="identity") +
geom_bar(data=df[df$sex=="Female",], aes(y=-values, fill=bmi), stat="identity") +
geom_hline(yintercept=0, colour="white", lwd=1) +
coord_flip(ylim=c(-101,101)) +
scale_y_continuous(breaks=seq(-100,100,50), labels=c(100,50,0,50,100)) +
labs(y="Percent", x="Country") +
ggtitle("Female Male")

Related

R ggplot facet_wrap with different y-axis labels, one values, one percentages

I'm plotting a time series value with its percentages using facet_wrap in ggplot:
For the plot below, the upper plot is the value, and the lower plot is percentage change. And I would like the y-axis in the lower plot to be "%". Normally in ggplot I would do something like
+ scale_y_continuous(labels = scales::percent)
But since I'm using facet_wrap, how do I specify that I only want one of the 2 plots' y-axis label to be percentages?
P.S. Here is the code to generate this plot:
library(data.table)
library(ggplot2)
library(scales)
library(dplyr)
pct <- function(x) {x/lag(x)-1}
Dates = seq(from = as.Date("2000-01-01"),
to =as.Date("2018-10-01"),
by = "1 month")
set.seed(1024)
this_raw = data.frame(CM = Dates,
value = rnorm(n = length(Dates)),
variable = rep("FAKE",length(Dates)))
this_diff = na.omit(as.data.table(this_raw %>%
group_by(variable) %>%
mutate_each(funs(pct), c(value))))
this_diff$type = "PerCng"
this_raw$type = "RAW"
plot_all = rbindlist(list(this_raw,this_diff))
plot_all$type = factor(plot_all$type, levels = c("RAW", "PerCng"))
out_gg = plot_all %>%
ggplot(aes(x=CM, y=value)) +
geom_line(color = "royalblue3") +
theme(legend.position='bottom')+
ggtitle("FAKE DATA") +
facet_wrap(~ type, scale = "free_y", nrow = 2,
strip.position = "left",
labeller = as_labeller(c(RAW = "Original", PerCng = "% Change") ) )+
scale_x_date(date_breaks = "12 month", date_labels = "%Y-%m",
date_minor_breaks = "3 month")+
ylab("")+
theme(plot.title = element_text(hjust = 0.5,size = 12),
axis.text.x = element_text(size = 6,angle = 45, hjust = 1),
axis.text.y = element_text(size = 6),
axis.title.y = element_text(size = 6)) +
theme(strip.background = element_blank(),
strip.placement = "outside")+
theme(legend.title=element_blank())
print(out_gg)
I agree with the above comments that facets are really not intended for this use case. Aligning separate plots is the orthodox way to go.
That said, if you already have a bunch of nicely formatted ggplot objects, and really don't want to refactor the code just for axis labels, you can convert them to grob objects and dig underneath the hood:
library(grid)
# Convert from ggplot object to grob object
gp <- ggplotGrob(out_gg)
# Optional: Plot out the grob version to verify that nothing has changed (yet)
grid.draw(gp)
# Also optional: Examine the underlying grob structure to figure out which grob name
# corresponds to the appropriate y-axis label. In this case, it's "axis-l-2-1": axis
# to the left of plot panels, 2nd row / 1st column of the facet matrix.
gp[["layout"]]
gtable::gtable_show_layout(gp)
# Some of gp's grobs only generate their contents at drawing time.
# Using grid.force replaces such grobs with their drawing time content (if you check
# your global environment, the size of gp should increase significantly after running
# the grid.force line).
# This step is necesary in order to use gPath() to generate the path to nested grobs
# (& the text grob for y-axis labels is nested rather deeply inside the rabbit hole).
gp <- grid.force(gp)
path.to.label <- gPath("axis-l-2", "axis", "axis", "GRID.text")
# Get original label
old.label <- getGrob(gTree = gp,
gPath = path.to.label,
grep = TRUE)[["label"]]
# Edit label values
new.label <- percent(as.numeric(old.label))
# Overwrite ggplot grob, replacing old label with new
gp = editGrob(grob = gp,
gPath = path.to.label,
label = new.label,
grep = TRUE)
# plot
grid.draw(gp)

How can I plot 2 related variables on the same axis using ggplot? [duplicate]

Edit: This question has been marked as duplicated, but the responses here have been tried and did not work because the case in question is a line chart, not a bar chart. Applying those methods produces a chart with 5 lines, 1 for each year - not useful. Did anyone who voted to mark as duplicate actually try those approaches on the sample dataset supplied with this question? If so please post as an answer.
Original Question:
There's a feature in Excel pivot charts which allows multilevel categorical axes.I'm trying to find a way to do the same thing with ggplot (or any other plotting package in R).
Consider the following dataset:
set.seed(1)
df=data.frame(year=rep(2009:2013,each=4),
quarter=rep(c("Q1","Q2","Q3","Q4"),5),
sales=40:59+rnorm(20,sd=5))
If this is imported to an Excel pivot table, it is straightforward to create the following chart:
Note how the x-axis has two levels, one for quarter and one for the grouping variable, year. Are multilevel axes possible with ggplot?
NB: There is a hack with facets that produces something similar, but this is not what I'm looking for.
library(ggplot2)
ggplot(df) +
geom_line(aes(x=quarter,y=sales,group=year))+
facet_grid(.~year,scales="free")
New labels are added using annotate(geom = "text",. Turn off clipping of x axis labels with clip = "off" in coord_cartesian.
Use theme to add extra margins (plot.margin) and remove (element_blank()) x axis text (axis.title.x, axis.text.x) and vertical grid lines (panel.grid.x).
library(ggplot2)
ggplot(data = df, aes(x = interaction(year, quarter, lex.order = TRUE),
y = sales, group = 1)) +
geom_line(colour = "blue") +
annotate(geom = "text", x = seq_len(nrow(df)), y = 34, label = df$quarter, size = 4) +
annotate(geom = "text", x = 2.5 + 4 * (0:4), y = 32, label = unique(df$year), size = 6) +
coord_cartesian(ylim = c(35, 65), expand = FALSE, clip = "off") +
theme_bw() +
theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())
See also the nice answer by #eipi10 here: Axis labels on two lines with nested x variables (year below months)
The suggested code by Henrik does work and helped me a lot! I think the solution has a high value. But please be aware, that there is a small misstake in the first line of the code, which results in a wrong order of the data.
Instead of
... aes(x = interaction(year,quarter), ...
it should be
... aes(x = interaction(quarter,year), ...
The resulting graphic has the data in the right order.
P.S. I suggested an edit (which was rejected until now) and, due to a small lack of reputation, I am not allowed to comment, what I rather would have done.
User Tung had a great answer on this thread
library(tidyverse)
library(lubridate)
library(scales)
set.seed(123)
df <- tibble(
date = as.Date(41000:42000, origin = "1899-12-30"),
value = c(rnorm(500, 5), rnorm(501, 10))
)
# create year column for facet
df <- df %>%
mutate(year = as.factor(year(date)))
p <- ggplot(df, aes(date, value)) +
geom_line() +
geom_vline(xintercept = as.numeric(df$date[yday(df$date) == 1]), color = "grey60") +
scale_x_date(date_labels = "%b",
breaks = pretty_breaks(),
expand = c(0, 0)) +
# switch the facet strip label to the bottom
facet_grid(.~ year, space = 'free_x', scales = 'free_x', switch = 'x') +
labs(x = "") +
theme_classic(base_size = 14, base_family = 'mono') +
theme(panel.grid.minor.x = element_blank()) +
# remove facet spacing on x-direction
theme(panel.spacing.x = unit(0,"line")) +
# switch the facet strip label to outside
# remove background color
theme(strip.placement = 'outside',
strip.background.x = element_blank())
p

Add space between specific facets in ggplot2 (facet_grid)

I've been able to add vertical space between all facets (Alter just horizontal spacing between facets (ggplot2)) but haven't been able to add just one space between specified facets?
Here's an example based on my real data (in the real plot I have stacked bars):
mydf<-data.frame(year = rep(c(2016,2016,2016,2016,2016,2016,2017,2017,2017,2017,2017,2017),times = 2),
Area = rep(c('here','there'),times = 12),
yearArea = rep(c('here.2016','here.2017', 'there.2016','there.2017'), times = 12),
treatment = rep(c('control','control','control','treat', 'treat','treat'), times = 4),
response = rep(c('a','b','c','d'), times = 6),
count = rep(c(23,15,30,20), times = 6))
mycolour<-c("#999999", "#0072B2", "#009E73","#000000")
Returns plot:
#default facet spacing
example<-ggplot(data=mydf, aes(x=treatment, y=count, fill=response)) +
geom_bar(stat="identity", width = 0.5) +
scale_fill_manual(values = mycolour, name = "Response") +
labs (y = "Count") +
facet_grid(~yearArea) +
theme_bw()
example
#spacing between each facet
spacedex<-example + theme(panel.spacing.x=unit(2, "lines"))
spacedex
How can I limit the addition of space to only between the second and third facet? (between here.2017 and there.2016)
library(grid)
gt = ggplot_gtable(ggplot_build(example))
gt$widths[7] = 4*gt$widths[7]
grid.draw(gt)

How can I remove spacing in Pie charts in multi panel and have only one legend at the top using r

The pie charts:
THE LINK TO DATA: https://drive.google.com/file/d/0BwoPt0jyGdzORkM3cVA0WjJodVk/view?usp=sharing
Mydata<-read.csv(file="final_analysis_candy_analysis.csv",head=TRUE,sep=",")
dhfr.Arg <- table(Mydata$dhfr.Arg.59.163.137.)
dhfr.Ile <- table(Mydata$dhfr.Ile.51.214.65.)
dhfr.Asn108 <- table(Mydata$dhfr.Asn108.328.372.)
glu.540 <- table(Mydata$glu.540.538.326.200.)
gly.437 <- table(Mydata$gly.437.848.300.)
library(plotrix)
par(op)
op <-par(mfrow=c(2,3),mar=c(0,0,1,0))
pct <- round(dhfr.Arg/sum(dhfr.Arg)*100)
lbls <- paste(names(dhfr.Arg), pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
lp<-pie3D(dhfr.Arg,radius=0.8,labels=lbls,explode=0.1,
labelrad=1.4,main="dhfr Arg 59(163,137)")
pct <- round(dhfr.Ile/sum(dhfr.Ile)*100)
lbls <- paste(names(dhfr.Ile), pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
lp<-pie3D(dhfr.Ile,radius=0.8,labels=lbls,explode=0.1,
labelrad=1.4,main="dhfr Ile 51(214,65)")
pct <- round(dhfr.Asn108/sum(dhfr.Asn108)*100)
lbls <- paste(names(dhfr.Asn108), pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
lp <- pie3D(dhfr.Asn108,radius=0.8,labels=lbls,explode=0.1,
labelrad=1.4,main="dhfr Asn108(328,372)")
pct <- round(glu.540/sum(glu.540)*100)
lbls <- paste(names(glu.540), pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
lp<-pie3D(glu.540,radius=0.8,labels=lbls,explode=0.1,
labelrad=1.4,main="glu 540(538,326,200)")
pct <- round(gly.437/sum(gly.437)*100)
lbls <- paste(names(gly.437), pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
lp <- pie3D(gly.437,radius=0.8,labels=lbls,explode=0.1,
labelrad=1.4,main="gly 437(848,300)")
par(op)
Using a 2D visualization will your plot much easier to comprehend. Therefore, an alternative 2D solution with ggplot:
# load needed packages
library(data.table)
library(ggplot2)
library(scales)
# process & summarise the data (with data.table)
mydat <- melt(setDT(Mydata),
id=1,
measure.vars=4:8)[, .N, by = .(variable,value)
][, `:=` (perc = round(N/sum(N),2),
pos = cumsum(N)-0.5*N), by = variable]
# create the plot with ggplot2 & scales
ggplot(mydat) +
geom_bar(stat="identity", aes(x="", y=N, fill=value)) +
geom_text(aes(x = "", y = pos, label = percent(perc))) +
scale_x_discrete(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
coord_polar(theta = "y") +
facet_grid(.~ variable, scales = "free") +
theme_minimal(base_size = 16) +
theme(axis.title = element_blank(), axis.text = element_blank(),
panel.grid = element_blank(), legend.title = element_blank())
which gives the following plot:
See this answer for how to calculate the pos variable with base R or with the plyr and dplyr packages.
However, pie-charts are mostly not the best way to visualize data. Also in this case a bart chart will result in a clearer visualization. With:
ggplot(mydat, aes(x=variable, y=perc, fill=value)) +
geom_bar(stat="identity", aes(label = percent(perc)), width=0.6) +
scale_y_continuous(labels = percent(c(0,0.25,0.50,0.75,1.00))) +
coord_flip() +
theme_minimal(base_size = 14) +
theme(axis.title = element_blank(), legend.title = element_blank())
you get:
From plotrix documentation, there is a margin parameter mar - Margins around the pie. See default is set to mar=c(4,4,4,4) which is creating this white space.
pie3D(x,edges=NA,radius=1,height=0.1,theta=pi/6,start=0,border=par("fg"),
col=NULL,labels=NULL,labelpos=NULL,labelcol=par("fg"),labelcex=1.5,
labelrad=1.25,sector.order=NULL,explode=0,shade=0.8,mar=c(4,4,4,4),pty="s",...)
Try setting mar to smaller amounts, so add in your pie3D calls this option, e.g. mar =c(1,1,1,1) to lower all the margins.

Is there a way to have a barplot and a stacked barplot on the same graph using barplot or ggplot?

I have two pieces of data that I want to overlay onto the same plot. I've looked at several ggplot articles and I don't think it's possible within ggplot. So I have been using barplot. I have 5 tiers and I'm plotting total dollars by tier as a solid bar.
Then I have another piece of data that represents the number of tasks within those tiers by two different types of workers. I have this as a stacked bar plot. But I want to show them on the same graph with the total dollar amount as one bar and then the corresponding stacked bar next to it.
Here are the plots:
The data for the first graph looks like this (it's a table):
1 2 3 4 5
0 9 340 97 812 4271
1 1 417 156 3163 11314
The data for the second graph looks like this (this is a dataset):
Tier variable value
1 1 Opp_Amt 16200.00
2 2 Opp_Amt 116067.50
3 3 Opp_Amt 35284.12
4 4 Opp_Amt 278107.10
5 5 Opp_Amt 694820.29
I want to put the graphs on top of each other but the bars keep overlapping and I want them to appear side by side by tier.
Code for what I have so far.
par(mar=c(2.5, 4, 4, 4)+2)
## Plot first set of data and draw its axis
barplot(data1$value, axes=FALSE,ylim=c(0,700000), xlab="", ylab="",
col="black",space=-10,main="Work Score")
axis(2, ylim=c(0,700000),col="black",las=1) ## las=1 makes horizontal labels
mtext("Total Opportunity Amount",side=2,line=3.5)
box()
## Allow a second plot on the same graph
par(new=TRUE)
## Plot the second plot and put axis scale on right
m <- barplot(counts, xlab="", ylab="", ylim=c(0,16000),axes=FALSE, col=c("red","darkblue"),space=3,width=0.5,density=20)
## a little farther out (line=4) to make room for labels
mtext("Task Ratio: Outbound to AE",side=4,col="red",line=3.5)
axis(4, ylim=c(0,16000), col="red",col.axis="black",las=1)
And it gives me this
Using ggplot, I would do something like one of these. They plot the two sets of data separately. The first arranges the data into one dataframe, then uses facet_wrap() to position the plots side-by-side. The second generates the two plot objects separately, then combines the two plots and the legend into a combined plot.
But if you really need the "dual y-axis" approach, then with some fiddling, and using the plots' layouts and gtable functions, it can be done (using code borrowed from here).
Like this:
library(ggplot2)
library(gtable)
library(plyr)
df1 <- data.frame(Tier = rep(1:5, each = 2),
y = c(9, 1, 340, 417, 97, 156, 812, 3063, 4271, 11314),
gp = rep(0:1, 5))
df2 <- read.table(text = "
Tier variable value
1 Opp_Amt 16200.00
2 Opp_Amt 116067.50
3 Opp_Amt 35284.12
4 Opp_Amt 278107.10
5 Opp_Amt 694820.29", header = TRUE)
dfA = df1
dfB = df2
names(dfA) = c("Tier", "Value", "gp")
dfA$var = "Task Ratio"
dfB = dfB[,c(1,3)]
dfB$gp = 3
dfB$var = "Total Opportunity Amount"
names(dfB) = names(dfA)
df = rbind(dfA, dfB)
df$var = factor(df$var)
df$var = factor(df$var, levels = rev(levels(df$var)))
ggplot(df, aes(Tier, Value, fill = factor(gp))) +
geom_bar(position = "stack", stat = "identity") +
facet_wrap( ~ var, scale = "free_y") +
scale_fill_manual("Group", breaks = c("0","1"), values = c("#F8766D", "#00BFC4", "black")) +
theme_bw() +
theme(panel.spacing = unit(2, "lines"),
panel.grid = element_blank())
Or this:
p1 <- ggplot(df1, aes(factor(Tier), y, fill = factor(gp))) +
geom_bar(position = "stack", stat = "identity") +
#guides(fill = FALSE) +
scale_y_continuous("Task Ratio",
limit = c(0, 1.1*max(ddply(df1, .(Tier), summarise, sum = sum(y)))),
expand = c(0,0)) +
scale_x_discrete("Tier") +
theme_bw() +
theme(panel.grid = element_blank())
p2 <- ggplot(df2, aes(factor(Tier), value)) +
geom_bar(stat = "identity") +
scale_y_continuous("Total Opportunity Amount", limit = c(0, 1.1*max(df2$value)), expand = c(0,0)) +
scale_x_discrete("Tier") +
theme_bw() +
theme(panel.grid = element_blank())
# Get the ggplot grobs,
# And get the legend from p1
g1 <- ggplotGrob(p1)
leg = gtable_filter(g1, "guide-box")
legColumn = g1$layout[which(g1$layout$name == "guide-box"), "l"]
g1 = g1[,-legColumn]
g2 <- ggplotGrob(p2)
# Make sure the width are the same in g1 and g2
library(grid)
maxWidth = unit.pmax(g1$widths, g2$widths)
g1$widths = as.list(maxWidth)
g2$widths = as.list(maxWidth)
# Combine g1, g2 and the legend
library(gridExtra)
grid.arrange(arrangeGrob(g2, g1, nrow = 1), leg,
widths = unit.c(unit(1, "npc") - leg$width, leg$width), nrow=1)
Or the dual y-axis approach (But not recommended for reasons given in #Phil's post):
width1 = 0.6 # width of bars in p1
width2 = 0.2 # width of bars in p2
pos = .5*width1 + .5*width2 # positioning bars in p2
p1 <- ggplot(df1, aes(factor(Tier), y, fill = factor(gp))) +
geom_bar(position = "stack", stat = "identity", width = width1) +
guides(fill = FALSE) +
scale_y_continuous("",
limit = c(0, 1.1*max(ddply(df1, .(Tier), summarise, sum = sum(y)))),
expand = c(0,0)) +
scale_x_discrete("Tier") +
theme_bw() +
theme(panel.grid = element_blank(),
axis.text.y = element_text(colour = "red", hjust = 0, margin = margin(l = 2, unit = "pt")),
axis.ticks.y = element_line(colour = "red"))
p2 <- ggplot(df2, aes(factor(Tier), value)) +
geom_blank() +
geom_bar(aes(x = Tier - pos), stat = "identity", width = width2) +
scale_y_continuous("", limit = c(0, 1.1*max(df2$value)), expand = c(0,0)) +
theme_bw() +
theme(panel.grid = element_blank())
# Get ggplot grobs
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
# Get locations of the panels in g1
pp1 <- c(subset(g1$layout, name == "panel", se = t:r))
## Get bars from g2 and insert them into the panel in g1
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]][[4]][[4]], pp1$t, pp1$l)
# Grab axis from g1, reverse elements, and put it on the right
index <- which(g1$layout$name == "axis-l")
grob <- g1$grobs[[index]]
axis <- grob$children[[2]]
axis$widths <- rev(axis$widths)
axis$grobs <- rev(axis$grobs)
axis$grobs[[1]]$x <- axis$grobs[[1]]$x - unit(1, "npc") + unit(3, "pt")
g <- gtable_add_cols(g, g1$widths[g1$layout[index, ]$l], pp1$r)
g <- gtable_add_grob(g, axis, pp1$t, pp1$l+1)
# Grab axis from g2, and put it on the left
index <- which(g2$layout$name == "axis-l")
grob <- g2$grobs[[index]]
axis <- grob$children[[2]]
g <- gtable_add_grob(g, rectGrob(gp = gpar(col = NA, fill = "white")), pp1$t-1, pp1$l-1, pp1$b+1)
g <- gtable_add_grob(g, axis, pp1$t, pp1$l-1)
# Add axis titles
# right axis title
RightAxisText = textGrob("Task Ratio", rot = 90, gp = gpar(col = "red"))
g <- gtable_add_cols(g, unit.c(unit(1, "grobwidth", RightAxisText) + unit(1, "line")), 5)
g <- gtable_add_grob(g, RightAxisText, pp1$t, pp1$r+2)
# left axis title
LeftAxisText = textGrob("Total Opportunity Amount", rot = 90)
g <- gtable_add_grob(g, LeftAxisText, pp1$t, pp1$l-2)
g$widths[2] <- unit.c(unit(1, "grobwidth", LeftAxisText) + unit(1, "line"))
# Draw it
grid.newpage()
grid.draw(g)
It appears you are trying to plot two variables on two different y scales on to one chart. I recommend against this, and this is considered bad practice. See, for example, #hadley 's (the author of ggplot2) answer here about a similar issue: https://stackoverflow.com/a/3101876/3022126
It is possible to plot two variables on one y axis if they have comparable scales, but the range of your two datasets do not greatly overlap.
Consider other visualisations, perhaps using two separate charts.
Try looking at the add parameter for barplot.
## Function to create alpha colors for illustration.
col2alpha <- function(col, alpha = 0.5) {
tmp <- col2rgb(col)
rgb(tmp[1]/255, tmp[2]/255, tmp[3]/255, alpha)
}
## Some fake data
dat1 <- data.frame(id = 1:4, val = c(10, 8, 6, 4))
dat2 <- data.frame(id = 1:4, val = c(4, 6, 8, 10))
barplot(dat1$val, col = col2alpha("blue"))
barplot(dat2$val, col = col2alpha("red"), add = TRUE)

Resources