Related
I want to create a dual axis plot in ggplot R with a dual bar and line plot, like this one created in excel.
The y axis scales are different.
my data is as follows;
I've created a bar plot and line plot. But unsure on how to put them together (I've tried man various ways and they don't seem to work).
Here is my code for the bar plot.
inf_conc <- ggplot(data=data, aes(x=Day, y=inf)) +
geom_bar(stat="identity", width=0.4, color="red3", fill="red3") +
ggtitle("Influent Microplastic Concentration \n and Flow Rate") +
# \n splits long titles into multiple lines
xlab("Day") +
ylab("Microplastic Concentration (MPs/L)") +
scale_y_continuous(limits =c(0, 50), breaks = seq(0, 50, 5))
inf_conc + theme(axis.text = element_text(size = 20, colour = "black"),
plot.title = element_text(size =25, hjust = 0.5,
face = "bold"), axis.title = element_text(size = 20,
face = "bold", margin = 5))
inf_conc + theme(axis.text = element_text(size = 20, colour = "black"),
plot.title = element_text (size =25, hjust = 0.5, face = "bold"),
axis.title = element_text(size = 20, face = "bold", margin = 20))
and here is the code for the line plot:
inf_flow <- ggplot(data=data, aes(x=Day, y=flow, group = 1)) +
geom_line(stat = "identity", colour ="blue4") +
geom_point(colour ="blue4") +
ylab("Inlet flow L/s")+
xlab("Day")+
scale_y_continuous(limits=c(0,800), breaks = seq(0, 800, 100))
inf_flow + theme(axis.text = element_text(size = 20, colour = "black"),
plot.title = element_text (size =25, hjust = 0.5, face = "bold"),
axis.title = element_text(size = 20, face = "bold", margin = 5))
inf_flow + theme(axis.text = element_text(size = 20,
colour = "black"), plot.title = element_text (size =25, hjust = 0.5,
face = "bold"), axis.title = element_text(size = 20, face = "bold",
margin = 20))
Can anyone help with how I can get these onto one dual axis graph please.
GGplot doesn't make it especially easy, but you can do it:
library(ggplot2)
my_dat <- data.frame(
Day = paste("Day",rep(1:3, each=3), rep(c("(AM)", "(Midday)", "(PM)"), 3), sep= " "),
day_num = 1:9,
inf = seq(from = 13,to = 45, length=9),
flow = runif(9, 580, 740)
)
ggplot() +
geom_bar(data=my_dat, aes(x=day_num, y=inf, fill = "Influent Concentration"), stat="identity", width=.6) +
geom_line(data=my_dat, aes(x=day_num, y=flow*(50/800), colour="FLow Rate. L/s")) +
scale_fill_manual(values="red") +
scale_colour_manual(values="blue") +
scale_x_continuous(breaks=1:9, labels=my_dat$Day) +
scale_y_continuous(sec.axis = sec_axis(trans = ~.x*800/50, name = "Flow Rate L/S"), limits = c(0,50), name = "Influent. MPs/L") +
labs(fill="", colour="", x="") +
theme(legend.position="bottom",
axis.text.x = element_text(angle=45, hjust=1))
Created on 2023-01-17 by the reprex package (v2.0.1)
The main things you have to do are to
Transform the second-axis series to have the same range(ish) as the first-series axis. In your case, the excel graph had the second y-axis going from 0-800 and the first y-axis going from 0-50, so the transformation is simple, you multiply the second series values by 50/800.
In the scale_y_continuou() function there is an argument sec.axis which allows you to plot a second axis on the right-hand side of the plot. Here, you need to specify the trans argument to transform the values you're plotting back into the original values. That's what trans = ~.x*800/50 does.
EDIT: Modifying OP's code
I modified your code as much as I can without actually having the data. The picture of the data that you provided does not give enough information about the data, if you use dput(data) and post the results, I could likely help more. For now, try this:
inf_plot <- ggplot(data=data, aes(x=Day))+
geom_bar(aes(y=inf, fill="Influent conc"), stat = "identity", width=0.4)+
geom_line(aes(y=flow*(50/800), colour="flow rate"), size = 1.4, group=1)+
ggtitle("Influent Microplastic Concentration \n and Influent Flow Rate")+
xlab("\n\nDay") +
ylab("Microplastic Concentration (MPs/L)\n\n")+
scale_fill_manual(values="red4") +
scale_colour_manual(values="blue4") +
scale_y_continuous(sec.axis = sec_axis(~.*800/50, name = "Inlet flow rate (L/s)\n\n"), limits = c(0,50))
inf_plot + theme(axis.text = element_text( size = 20, colour = "black"),
plot.title = element_text (size =25, hjust = 0.5, vjust = 5, face = "bold"),
axis.title = element_text (size = 20, face = "bold"),
plot.margin =unit(c(1.5, 1.5, 1.5, 1.5), "cm"),
legend.position = "bottom")
The answer was a great help in how to transform my axis.
Initially produced the graph a slightly different way, but incorporated the same transformation of axis.
However, I can't seem to get the legend to appear at the bottom of the graph with the following code.
inf_plot <- ggplot(data=data, aes(x=Day))+
geom_bar(aes(y=inf, fill="Influent conc"), stat = "identity", width=0.4,
colour="red4", fill = "red4")+
ggtitle("Influent Microplastic Concentration \n and Influent Flow Rate")+
xlab("\n\nDay") +
ylab("Microplastic Concentration (MPs/L)\n\n")+
geom_line(aes(y=flow*(50/800), colour="flow rate"), size = 1.4, colour ="blue4", group = 1)+
scale_fill_manual(values="red4") +
scale_colour_manual(values="blue4") +
scale_y_continuous(sec.axis = sec_axis(~.*800/50, name = "Inlet flow rate (L/s)\n\n"), limits = c(0,50))
inf_plot + theme(axis.text = element_text( size = 20, colour = "black"),
plot.title = element_text (size =25, hjust = 0.5, vjust = 5, face = "bold"),
axis.title = element_text (size = 20, face = "bold"),
plot.margin =unit(c(1.5, 1.5, 1.5, 1.5), "cm"),
legend.position = "bottom")
enter image description here
working with nfl data here.. trying gganimate for the first time... trying to use geom_point and geom_line but geom_line will not appear..
data frame here..
week = c(1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17)
plays= c(9,10,5,3,4,4,3,5,6,5,11,12,6,11,7,3)
mean_epa= c(.67, .27, -.5, -1.09, -.3, .68, -.72, -.32, 1.03, 1.05, .56, .17, -.61, -.05, -.14, 1.5)
CLEdrive1b <- data.frame(week,plays,mean_epa)
within week variable, 7 is missing (because browns did not have game week 7)... not sure if that is causing issues?
my first attempt below
p <- ggplot(CLEdrive1b, aes(x=as.factor(week),y=mean_epa)) +
theme_minimal() +
geom_point(aes(group = seq_along(week)), color = "orange", size = 4) +
geom_line(alpha = 0.5)+
transition_reveal(week)+
geom_text(aes(label = mean_epa))+
xlab("Week") +
ylab("EPA/Play") +
labs(title="Browns Opening Drives",
subtitle = "EPA/Drive by Week",
caption="Data from nflscrapR")+
theme(axis.title = element_text(size = 10),
axis.text = element_text(size = 9),
plot.title = element_text(size = 15),
plot.subtitle = element_text(size = 10),
plot.caption = element_text(size = 8))
animate(p)
alternative method I tried..
pi <- ggplot(CLEdrive1b, aes(week,y=mean_epa)) +
theme_minimal() +
geom_point(aes(group = seq_along(week)), color = "orange", size = 4) +
geom_line(alpha = 0.5)+
transition_reveal(week)+
geom_text(aes(label = mean_epa))+
xlab("Week") +
ylab("EPA/Play") +
labs(title="Browns Opening Drives",
subtitle = "EPA/Drive by Week",
caption="Data from nflscrapR")+
theme(axis.title = element_text(size = 10),
axis.text = element_text(size = 9),
plot.title = element_text(size = 15),
plot.subtitle = element_text(size = 10),
plot.caption = element_text(size = 8))
animate(pi)
removed as.factor from x variable (week).. and geom_line appears! but x-axis loses labels I am seeking and geom_text stops performing how it was above..
thank you for spending time to help...
The problem is that when converting it to factors, ggplot doesn't "know" any more how to group the week variable, and does not connect the observations any more. Adding aes(group = 1) to your geom_line is all you need.
library(gganimate)
#> Loading required package: ggplot2
library(ggplot2)
week = c(1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17)
plays= c(9,10,5,3,4,4,3,5,6,5,11,12,6,11,7,3)
mean_epa= c(.67, .27, -.5, -1.09, -.3, .68, -.72, -.32, 1.03, 1.05, .56, .17, -.61, -.05, -.14, 1.5)
CLEdrive1b <- data.frame(week,plays,mean_epa)
p <- ggplot(CLEdrive1b, aes(x=as.factor(week),y=mean_epa)) +
geom_point(aes(group = seq_along(week)), color = "orange", size = 4) +
geom_line(alpha = 0.5, aes(group = 1))+
transition_reveal(week)+
geom_text(aes(label = mean_epa))
animate(p)
Created on 2020-02-03 by the reprex package (v0.3.0)
I am trying to plot a 2y-axes plot; on the left, the actual values, and on the right, the % values. In addition to this, I need to apply coord_trans on the left y-axis for a better visualization of small values. However, when I do it, the labels on the right do not show up.
Here the data (example)
Here the code
DAXIS <- ggplot(x1, aes(hour, value_T, colour=season, linetype = variable)) +
geom_line(size = 1) +
scale_linetype_manual(c("var"), values=c("solid", "dashed", "dotted"))+ # here to change one name
geom_point(aes(shape = season), size = 1)+
labs(x = "hour", y = "T") +
scale_x_continuous(breaks = c(0, 6, 12, 18, 23), labels= c(0, 6, 12, 18, 24))+
scale_y_continuous("T",
sec.axis = sec_axis(~./2.341598, name = " [%] ",
breaks=c(0.2135294, 0.4270588, 0.6405882, 0.8541176,1),
labels = function(b) { paste0(round(b * 100, 0), "%")}))+
#coord_trans(y = "log10", breaks=c(0.5,1,1.5,1.903738), labels = c(0.5,1,1.5,1.9))+ # attemp 1
#coord_trans(y = "log10")+ # attemp 2
scale_color_aaas()+
theme_bw()+
theme(legend.direction = "horizontal", legend.position = "bottom", legend.key = element_blank(),
legend.background = element_rect(fill = "white", colour = "gray30")) +
theme(legend.position="bottom",
text=element_text(size=18),
axis.text.x = element_text(size=15),
axis.text.y = element_text(size=15))
DAXIS
Here the output without coord_trans
Here the output with coord_trans
Any help is very much appreciated
Considering the following data, I am able to generate a plot which describes how the risk of a react over a time interval changes.
risk_1 <- c(0.121,0.226,0.333,0.167,0.200,0.273,0.138,0.323,0.394,0.250,0.200,0.545,0.190,0.355,0.515,0.333,0.300,0.818)
risk_minus_SE <- c(0.060,0.114,0.198,0.047,0.057,0.097,0.072,0.186,0.247,0.089,0.057,0.280,0.109,0.211,0.352,0.138,0.108,0.523)
risk_plus_SE <- c(0.229,0.398,0.504,0.448,0.510,0.566,0.249,0.499,0.563,0.532,0.510,0.787,0.309,0.531,0.675,0.609,0.603,0.949)
Status <- rep(c(rep('With placebo',3),rep('With drug',3)),3)
durtn <- rep(c('(3-15]','(15-30]','(30-46]'),6)
react <- c(rep("x\u226516",6),rep("x\u226509",6),rep("x\u226504",6))
df1 <- data.frame(risk_1, risk_minus_SE, risk_plus_SE, Status, durtn, react)
dodge <- position_dodge(width=0.45)
ggplot(df1,aes(colour=react, y=risk_1, x=durtn)) +
geom_point(aes(shape=durtn), shape=16, size = 5, position=dodge) +
geom_errorbar(aes(ymin=risk_minus_SE, ymax=risk_plus_SE), position = dodge, width=0.5, size=1, lty=1) +
scale_colour_manual(values = c('black','red','blue')) +
facet_grid(~Status) +
scale_shape_manual(values = c(8,19))+
theme_bw() +
scale_x_discrete(limits=c('(3-15]','(15-30]','(30-46]')) +
coord_cartesian(ylim = c(0, 0.8)) +
theme(legend.position = c(.1, .85), legend.background = element_rect(colour = "black"),
plot.title = element_text(lineheight=1.5, face="bold", size=rel(1.5), hjust = 0.5),
panel.grid.major.x = element_blank(),
axis.text.x = element_text(vjust=0.5, size=16),
axis.text.y = element_text(vjust=0.5, size=16),
axis.title.y = element_text(size=20),
axis.title.x = element_text(size=20),
legend.text = element_text(size = 16, face = "bold"),
strip.text = element_text(size=25)) +
xlab("\ntime (min)") + ylab("Risk")
What I want to do is overlay a series of points at given x and y coordinates.
That being at With drug & durtn==(3,15], manually insert points at.....
Risk==0.5 for react=x≥04 in black
Risk==0.2 for react=x≥09 in red
Risk==0.0 for react=x≥16 in blue
Such that the desired output should look like
How does one use the geom_point() in combination with a facet_grid and dodge
First, you have to create a separate data frame that contains the data for the additional points.
dat <- data.frame(risk_1 = c(0.5, 0.2, 0),
react = levels(df1$react),
durtn = '(3-15]',
Status = 'With drug')
This new data frame dat can be used with geom_point to add an additional layer to the existing plot.
+ geom_point(data = dat, position = dodge, shape = 4, size = 5, show.legend = FALSE)
DataLink:
https://www.dropbox.com/s/ql5jw7eng3plrso/GTAP_MacroValueChange.csv
Code:
library(ggplot2)
library(grid)
#Upload data
ccmacrosims2 <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/GTAP-CGE/GTAP_NewAggDatabase/NewFiles/GTAP_MacroValueChange.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
#Data manipulation for analysis
ccmacrorsts2 <- as.data.frame(ccmacrosims2)
ccmacrorsts2[6:10] <- sapply(ccmacrorsts2[6:10],as.numeric)
ccmacrorsts2 <- droplevels(ccmacrorsts2)
ccmacrorsts2 <- transform(ccmacrorsts2,region=factor(region,levels=unique(region)))
#Selecting data of interest
GDPDecomp1 <- melt(ccmacrorsts2[ccmacrorsts2$region %in% c("TUR","MAR"), ])
GDPDecomp2 <- GDPDecomp1[GDPDecomp1$sres %in% c("AVERAGE"), ]
GDPDecomp.f <- subset(GDPDecomp2, variable !="GDP")
#Ploting
GDPDecompPlot <- ggplot(data = GDPDecomp.f, aes(factor(region),value, fill=variable))
GDPDecompPlot + geom_bar(stat="identity", position="stack") + facet_wrap(~tradlib, scales="free_y") +
theme(axis.text.x = element_text(colour = 'black', angle = 90, size = 12, hjust = 0.5, vjust = 0.5),axis.title.x=element_blank()) +
ylab("GDP (Change in $US million)") + theme(axis.text.y = element_text(colour = 'black', size = 12), axis.title.y = element_text(size = 12)) +
theme(strip.text.x = element_text(size = 12, hjust = 0.5, vjust = 0.5, face = 'bold'))
Issue at hand:
The barplot using ggplot (see below)
excel_barplot:
It seems that the plot does not really represent correctly the values of the data. What I am looking for is something like what we get from excel_barplot_sample. For instance, comparing the panel "TRLIBEU" under ggplot with its counterpart using excel, one can clearly notice that the ggplot does not capture the values in the data correctly when stacking.
Any help on how to correct for the discrepancy?
Thanks in advance
If you need to stack bars in ggplot2 with negative values then for better result you should make two new data frames - one for positive values and second for negative values.
GDPDecomp.f.pos<-GDPDecomp.f[GDPDecomp.f$value>0,]
GDPDecomp.f.neg<-GDPDecomp.f[GDPDecomp.f$value<0,]
Then use each data frame in its own geom_bar() call.
ggplot()+
geom_bar(data=GDPDecomp.f.pos,aes(x=factor(region),y=value,fill=variable),stat="identity")+
geom_bar(data=GDPDecomp.f.neg,aes(x=factor(region),y=value,fill=variable),stat="identity")+
facet_wrap(~tradlib, scales="free_y") +
theme(axis.text.x = element_text(colour = 'black', angle = 90, size = 12, hjust = 0.5, vjust = 0.5),axis.title.x=element_blank()) +
ylab("GDP (Change in $US million)") + theme(axis.text.y = element_text(colour = 'black', size = 12), axis.title.y = element_text(size = 12)) +
theme(strip.text.x = element_text(size = 12, hjust = 0.5, vjust = 0.5, face = 'bold'))