R - ggplot2 - bar chart - series get incorrect value labels - r

I am trying to plot a basic stack bar chart to present number of acceptations and rejections for n simulations. (one column)
How can I control which series gets on the top of the stack together with corresponding value label?
I tried two versions neither had worked. Either colors are wrong or the labels.
Version 1
#version 1
T <- c(1,0)
H0_Testing <- c("Accept","Reject")
Counter <- c(100,900)
Label= c("L","L")
barplotdata<- data.frame(H0_Testing,T,Counter,Label)
fill <- c("#E1B378","#5F9EA0")
chartlabels=c("Accepted1","Rejected1")
title="version 1"
#Ploting
ggplot(barplotdata,aes(x=Label,y=Counter,fill=factor(T))) + geom_bar(stat ="identity",width=.2)+
geom_text(data=barplotdata, aes(label =Counter, y = Counter, size=4), show_guide = F)+
scale_fill_manual(labels=chartlabels, values=fill) +
theme(legend.title = element_blank()) +
theme(plot.title = element_text(size = 10),
axis.title.x = element_text(face="bold",size = 9), axis.title.y = element_text(face="bold",size = 8),
axis.text.x = element_text(size=8),axis.text.y = element_text(size=7),legend.text = element_text(size=7.5))+
ggtitle(title)
Version 2
#version 2
T <- c(0,1)
H0_Testing <- c("Reject","Accept")
Counter <- c(900,100)
Label= c("L","L")
barplotdata<- data.frame(H0_Testing,T,Counter,Label)
fill <- c("#5F9EA0","#E1B378")
chartlabels=c("Rejected2","Accepted2")
title="version 2"
#Ploting
ggplot(barplotdata,aes(x=Label,y=Counter,fill=factor(T))) + geom_bar(stat ="identity",width=.2)+
geom_text(data=barplotdata, aes(label =Counter, y = Counter, size=4), show_guide = F)+
scale_fill_manual(labels=chartlabels, values=fill) +
theme(legend.title = element_blank()) +
theme(plot.title = element_text(size = 10),
axis.title.x = element_text(face="bold",size = 9), axis.title.y = element_text(face="bold",size = 8),
axis.text.x = element_text(size=8),axis.text.y = element_text(size=7),legend.text = element_text(size=7.5))+
ggtitle(title)

New Plot:
T <- c(0,1)
H0_Testing <- c("Reject","Accept")
Counter <- c(900,100)
Label= c("L","L")
barplotdata<- data.frame(H0_Testing,T,Counter,Label)
fill <- c("#5F9EA0","#E1B378")
chartlabels=c("Rejected2","Accepted2")
title="version 2"
ggplot(barplotdata,aes(x=Label,y=Counter,fill=rev(factor(Counter)))) + geom_bar(stat ="identity",width=.2)+
geom_text(data=barplotdata, aes(label =rev(factor(Counter)), size=4), show.legend = F)+
scale_fill_manual(labels=chartlabels, values=fill) +
theme(legend.title = element_blank()) +
theme(plot.title = element_text(size = 10),
axis.title.x = element_text(face="bold",size = 9), axis.title.y = element_text(face="bold",size = 8),
axis.text.x = element_text(size=8),axis.text.y = element_text(size=7),legend.text = element_text(size=7.5))+
ggtitle(title)
ggplot(barplotdata,aes(x=Label,y=Counter,fill=factor(Counter))) + geom_bar(stat ="identity",width=.2)+
geom_text(data=barplotdata, aes(label =rev(factor(Counter)), size=4), show.legend = F)+
scale_fill_manual(labels=c("Accepted2","Rejected2"), values=fill) +
theme(legend.title = element_blank()) +
theme(plot.title = element_text(size = 10),
axis.title.x = element_text(face="bold",size = 9), axis.title.y = element_text(face="bold",size = 8),
axis.text.x = element_text(size=8),axis.text.y = element_text(size=7),legend.text = element_text(size=7.5))+
ggtitle(title)
Finally, if you want to switch the tiles:
ggplot(barplotdata,aes(x=Label,y=rev(Counter),fill=factor(Counter))) + geom_bar(stat ="identity",width=.2)+
geom_text(data=barplotdata, aes(label =rev(factor(Counter)), size=4), show.legend = F)+
scale_fill_manual(labels=c("Rejected2","Accepted2"), values=fill) +
theme(legend.title = element_blank()) +
theme(plot.title = element_text(size = 10),
axis.title.x = element_text(face="bold",size = 9), axis.title.y = element_text(face="bold",size = 8),
axis.text.x = element_text(size=8),axis.text.y = element_text(size=7),legend.text = element_text(size=7.5))+
ggtitle(title)
Note, for this third plot I added y=rev(Counter) in the aesthetics call.
as opposed to (your original plot Version 2):
#Ploting
ggplot(barplotdata,aes(x=Label,y=Counter,fill=factor(T))) + geom_bar(stat ="identity",width=.2)+
geom_text(data=barplotdata, aes(label =Counter, y = Counter, size=4), show.legend = F)+
scale_fill_manual(labels=chartlabels, values=fill) +
theme(legend.title = element_blank()) +
theme(plot.title = element_text(size = 10),
axis.title.x = element_text(face="bold",size = 9), axis.title.y = element_text(face="bold",size = 8),
axis.text.x = element_text(size=8),axis.text.y = element_text(size=7),legend.text = element_text(size=7.5))+
ggtitle(title)
The key difference for the bars is: fill=rev(factor(T) -notice that i reversed the factor level with the rev() command. For the text, notice that I removed y=Counter from geom_text() and changed the label value to rev(factor(Counter)). Also, for the second plot I manually set the legend items.
UPDATE
As per the OP's request in the comments, "I would like to change two more formats, decrease the font of the value labels and get rid of the T below x axis. Do you know how could I do the formatting?"
To decrease the font size, move size out of the aesthetics (you can also get rid of show.legend=F or show_guide=F). To get rid of the axis label you would add theme(axis.ticks = element_blank(), axis.text.x = element_blank())+ylab("Counter") -that removes the letter L and the tick mark which is what I think you meant when you said T. The code for both within the ggplot call is:
ggplot(barplotdata,aes(x=Label,y=rev(Counter),fill=factor(Counter))) + geom_bar(stat ="identity",width=.2)+
geom_text(data=barplotdata, aes(label =rev(factor(Counter))),size=2)+
scale_fill_manual(labels=c("Rejected2","Accepted2"), values=fill) +
theme(legend.title = element_blank()) +
theme(plot.title = element_text(size = 10),
axis.title.x = element_text(face="bold",size = 9), axis.title.y = element_text(face="bold",size = 8),
axis.text.x = element_text(size=8),axis.text.y = element_text(size=7),legend.text = element_text(size=7.5))+
ggtitle(title)+theme(axis.ticks = element_blank(), axis.text.x = element_blank())+ylab("Counter")

Related

R Hourly Heatmap with adjusted Dates

I'm new to r and tried the hourly heatmapt from the r grah gallery:
https://r-graph-gallery.com/283-the-hourly-heatmap.html
My question is, if it possible to adjust it that way, that the x axis ish shown with the correct amount of days in the month(example: Jan 1 … 31, Feb 1 … 28)
I tried to change scale_x_continuos with scale_x_date but it didnt worked as expected.
You can amend the code as folows, using a custom labelling function in scale_x_continuous:
ggplot(df, aes(day, hour, fill = temp)) +
geom_tile(color = "white",size = 0.1) +
scale_fill_viridis(name = "Hrly Temps C", option = "C") +
facet_grid(year~month, scales = 'free_x') +
scale_y_continuous(trans = "reverse", breaks = unique(df$hour)) +
scale_x_continuous(breaks = ~c(1, 10, 20, floor(max(.x, na.rm = TRUE)) -2)) +
theme_minimal(base_size = 8) +
labs(title= paste("Hourly Temps - Station", statno), x = "Day",
y = "Hour Commencing") +
theme(legend.position = "bottom",
plot.title = element_text(size = 14, hjust = 0),
axis.text.y = element_text(size = 6),
strip.background = element_rect(colour = "white"),
axis.ticks = element_blank(),
axis.text = element_text(size = 7),
legend.title = element_text(size = 8),
legend.text = element_text(size = 6)) +
removeGrid()

ggplot: Adding a plot outline with the axis ticks

I'm trying to create plots where the x-axis ALSO appears above the plot as well as the y-axis to the right of the plot. Both should contain the same ticks as the normal axis, but NOT the axis text. This should result in a "box" with helpful tick-marks around the plot. I would also like to have smaller ticks in-between my major ticks (that are labelled) that do not have a label. Here is a figure I made:
I also drew in pink what I would like to achieve in R:
]3
My code for this plot:
p <- p + xlab("") + ylab("") + theme(legend.position = "none") + theme(axis.ticks.length = unit(-0.25, "cm"), axis.text.x = element_text(size = 30, hjust=1)) + theme(axis.text.y = element_text(size=35, hjust = 1), strip.text = element_text(size=35), axis.title.y = element_text(size = 40), legend.text = element_text(size=30), axis.title.x = element_text(size=40), legend.title = element_text(size=45))
p <- p + theme(text = element_text(family = "Helvetica")) + scale_x_continuous(limits=c(-0.5, 25), breaks = c(0, 2, 4, 6, 8, 24)) + theme(legend.background = element_rect(color = "black", linetype = "solid")) + scale_colour_manual(values = cbpallette)
p <- p + theme(legend.key.size = unit(2.5, "cm")) + theme(axis.text.x = element_text(margin = margin(t = .5, unit = "cm")), axis.text.y = element_text(margin = margin(r = .5, unit = "cm")))
p
*** Edit ***
Here is updated code, using Stefans advise. The figure looks like so, the ticks are there, the axis are missing:
p <- p + xlab("") + ylab("") + theme(legend.position = "none") + theme(axis.ticks.length = unit(-0.25, "cm"), axis.text.x = element_text(size = 30, hjust=1)) + theme(axis.text.y = element_text(size=35, hjust = 1, angle=45), strip.text = element_text(size=35), axis.title.y = element_text(size = 40), legend.text = element_text(size=30), axis.title.x = element_text(size=40))
p <- p + theme(text = element_text(family = "Helvetica")) + scale_colour_manual(values = cbpallette)
p <- p + theme(axis.text.x = element_text(margin = margin(t = .5, unit = "cm")), axis.text.y = element_text(margin = margin(r = .5, unit = "cm")))
p
You could duplicate the axes using argument sec.axis = dup_axis() for both scales like so:
library(ggplot2)
ggplot(mtcars, aes(hp, mpg)) +
geom_point() +
scale_x_continuous(sec.axis = dup_axis(name = NULL, labels = NULL)) +
scale_y_continuous(sec.axis = dup_axis(name = NULL, labels = NULL))

plotting p-values using ggplot stat_summary

I want to plot a dataframe (stats) with the coefficient and error bars, and automatically write the p-values above each point.
stats <- data.frame(Coefficient = c(-0.07,-0.04,-0.15173266),
p_value = c(.0765210755,0.5176050652,0.0001309025),
conf_low = c(-.1544418,-0.1686583,-0.2294873),
conf_high = c(0.007812205,0.084939487,-0.073978033),
Test = c("TestA","TestB","TestC"))
I am trying to make a function to plot the p-values above each Coefficient point. (The coord_flip in the plot below may also be throwing me off.
give.pval <- function(y){
return(c(x = Coefficient, label = stats$p_value))
}
The following ggplot is exactly what I need, except for the stat_summary line which I am doing incorrectly
ggplot(stats, aes(x = Test, y = Coefficient)) +
geom_point(aes(size = 6)) +
geom_errorbar(aes(ymax = conf_high, ymin = conf_low)) +
geom_hline(yintercept=0, linetype="dashed") +
#stat_summary(fun.data = give.pval, geom = "text") +
theme_calc() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.text.x = element_text(size = 12, vjust = 0.5), axis.title.x = element_text(size = 16),
axis.text.y = element_text(size = 12), axis.title.y = element_blank(),
legend.position = "none",
plot.title = element_text(hjust = 0.5, size = 24)) +
coord_flip() +
ylab("Coefficient")
I would like the have this plot but with the appropriate p-value above each of the three Coefficient points.
Thanks for any advice.
This could be achieved with a geom_text layer where you map p_value on the label aes and some additional nudging
library(ggplot2)
stats <- data.frame(Coefficient = c(-0.07,-0.04,-0.15173266),
p_value = c(.0765210755,0.5176050652,0.0001309025),
conf_low = c(-.1544418,-0.1686583,-0.2294873),
conf_high = c(0.007812205,0.084939487,-0.073978033),
Test = c("TestA","TestB","TestC"))
ggplot(stats, aes(x = Test, y = Coefficient)) +
geom_point(aes(size = 6)) +
geom_errorbar(aes(ymax = conf_high, ymin = conf_low)) +
geom_hline(yintercept=0, linetype="dashed") +
geom_text(aes(label = p_value), nudge_x = .2) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.text.x = element_text(size = 12, vjust = 0.5), axis.title.x = element_text(size = 16),
axis.text.y = element_text(size = 12), axis.title.y = element_blank(),
legend.position = "none",
plot.title = element_text(hjust = 0.5, size = 24)) +
coord_flip() +
ylab("Coefficient")

R ggplot2: how to add legend if I use dataframe as data input?

I have tried any method online but the legend is not automatically showing up. Some previous issue says the usage of dataframe in ggplot2 is not preferable, but I have tried together() method and it doesn't work either. How to fix the bug?
Here is the data:
library(ggplot2)
library(gtable)
R = 0.01*c(7.000, 6.800, 6.620, 6.460, 6.330, 6.250, 6.200, 6.160, 6.125, 6.100)
Maturity = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
c = seq(from=0, to=0, length.out = length(R))
for (i in 1:length(R)){
for (j in 1:i){
c[i] = c[i] + 1/(1+R[j])^Maturity[j]
}
c[i] = (1-1/(1+R[i])^Maturity[i])/c[i]
}
Forward = seq(from=0, to=0, length.out = length(R))
for (i in 1:length(R)){
Forward[i] = (1+R[i+1])^2/(1+R[i])-1
}
Here is the plot code in RMarkDown:
```{R fig.width=2.7559, fig.height=2.0669291}
df = data.frame(Maturity=Maturity, ParYield=c, ForwardRate=Forward, R=R)
p = ggplot(data=df, aes(x=Maturity, y=ParYield)) +
ggtitle("Curve of Zero-coupon Yield, Par Yield, Forward Yield")+
labs(x = "Maturity (year)", y = "Par Yield") +
scale_y_continuous(breaks=seq(100*(min(na.omit(Forward), c)-0.01),
100*(max(na.omit(Forward),c)+0.01),by=0.1)/100,
sec.axis = dup_axis(),
labels = scales::number_format(accuracy = 0.005)) +
scale_x_continuous("Maturity", labels=as.character(Maturity), breaks=Maturity,
sec.axis = dup_axis()) +
theme_classic() +
geom_line(aes(y=ParYield), size=1) +
geom_line(aes(y=R), size=1) +
geom_line(aes(y=ForwardRate), size=1) +
geom_point(aes(y=ParYield), shape=21, fill=rgb(69/255, 117/255, 180/255),
size=3, stroke=1.5, color="black") +
geom_point(aes(y=R), shape=22, fill=rgb(145/255, 191/255, 219/255),
size=3, stroke=1.5, color="black") +
geom_point(aes(y=ForwardRate), shape=23, fill=rgb(224/255, 243/255, 248/255),
size=3, stroke=1.5, color="black") +
theme(plot.title = element_text(size=14, hjust=0.5),
text = element_text(size=15, colour = "black", family = "Calibri"),
axis.ticks.length = unit(-0.25, 'cm'),
axis.line = element_line(size=1),
axis.ticks = element_line(size=1),
axis.text.x = element_text(margin = margin(t=15)),
axis.text.x.top = element_text(margin = margin(b=15)),
axis.text.y.right = element_text(margin = margin(l=15, r=5)),
axis.text.y = element_text(margin = margin(l=5, r=15)),
axis.title.x.top = element_blank(),
axis.title.y.right = element_blank())+
guides(colour=guide_legend(override.aes = list(pch=c(16,21,20),fill=c('r','r','r'))))+
theme(legend.position = c(0.8,0.8), legend.justification = c("right", "top"))
p
Check this sketch and make the necessary adjustments as I am not clear on how colors must be set. Pay attention to the suggestion from #stefan and modify next code:
library(ggplot2)
#Code
df = data.frame(Maturity=Maturity, ParYield=c, ForwardRate=Forward, R=R)
ggplot(data=df, aes(x=Maturity, y=ParYield)) +
ggtitle("Curve of Zero-coupon Yield, Par Yield, Forward Yield")+
labs(x = "Maturity (year)", y = "Par Yield") +
scale_y_continuous(breaks=seq(100*(min(na.omit(Forward), c)-0.01),
100*(max(na.omit(Forward),c)+0.01),by=0.1)/100,
sec.axis = dup_axis(),
labels = scales::number_format(accuracy = 0.005)) +
scale_x_continuous("Maturity", labels=as.character(Maturity), breaks=Maturity,
sec.axis = dup_axis()) +
theme_classic() +
geom_line(aes(y=ParYield), size=1) +
geom_line(aes(y=R), size=1) +
geom_line(aes(y=ForwardRate), size=1) +
geom_point(aes(y=ParYield,colour='ParYield'), shape=21, fill=rgb(69/255, 117/255, 180/255),
size=3, stroke=1.5,show.legend = T) + #Black
geom_point(aes(y=R,colour='R'), shape=22,
fill=rgb(145/255, 191/255, 219/255),
size=3, stroke=1.5) + #Black
geom_point(aes(y=ForwardRate,colour='ForwardRate'), shape=23,
fill=rgb(224/255, 243/255, 248/255),
size=3, stroke=1.5) +
theme(plot.title = element_text(size=14, hjust=0.5),
text = element_text(size=15, colour = "black", family = "Calibri"),
axis.ticks.length = unit(-0.25, 'cm'),
axis.line = element_line(size=1),
axis.ticks = element_line(size=1),
axis.text.x = element_text(margin = margin(t=15)),
axis.text.x.top = element_text(margin = margin(b=15)),
axis.text.y.right = element_text(margin = margin(l=15, r=5)),
axis.text.y = element_text(margin = margin(l=5, r=15)),
axis.title.x.top = element_blank(),
axis.title.y.right = element_blank())+
guides(colour=guide_legend(override.aes = list(pch=c(16,21,20))))+
theme(legend.position = c(0.8,0.8), legend.justification = c("right", "top"))+
labs(color='Variable')
Output:
Other option would be:
#Code 2
df = data.frame(Maturity=Maturity, ParYield=c, ForwardRate=Forward, R=R)
ggplot(data=df, aes(x=Maturity, y=ParYield)) +
ggtitle("Curve of Zero-coupon Yield, Par Yield, Forward Yield")+
labs(x = "Maturity (year)", y = "Par Yield") +
scale_y_continuous(breaks=seq(100*(min(na.omit(Forward), c)-0.01),
100*(max(na.omit(Forward),c)+0.01),by=0.1)/100,
sec.axis = dup_axis(),
labels = scales::number_format(accuracy = 0.005)) +
scale_x_continuous("Maturity", labels=as.character(Maturity), breaks=Maturity,
sec.axis = dup_axis()) +
theme_classic() +
geom_line(aes(y=ParYield), size=1) +
geom_line(aes(y=R), size=1) +
geom_line(aes(y=ForwardRate), size=1) +
geom_point(aes(y=ParYield,colour='ParYield'), shape=21,
fill=rgb(69/255, 117/255, 180/255),
size=3, stroke=1.5,show.legend = T) +
geom_point(aes(y=R,colour='R'), shape=22,
fill=rgb(145/255, 191/255, 219/255),
size=3, stroke=1.5) +
geom_point(aes(y=ForwardRate,colour='ForwardRate'), shape=23,
fill=rgb(224/255, 243/255, 248/255),
size=3, stroke=1.5) +
scale_color_manual(values = c('black','black','black'))+
theme(plot.title = element_text(size=14, hjust=0.5),
text = element_text(size=15, colour = "black", family = "Calibri"),
axis.ticks.length = unit(-0.25, 'cm'),
axis.line = element_line(size=1),
axis.ticks = element_line(size=1),
axis.text.x = element_text(margin = margin(t=15)),
axis.text.x.top = element_text(margin = margin(b=15)),
axis.text.y.right = element_text(margin = margin(l=15, r=5)),
axis.text.y = element_text(margin = margin(l=5, r=15)),
axis.title.x.top = element_blank(),
axis.title.y.right = element_blank())+
guides(colour=guide_legend(override.aes = list(pch=c(16,21,20))))+
theme(legend.position = c(0.8,0.8), legend.justification = c("right", "top"))+
labs(color='Variable')
Output:
You need to map some additional aesthetics to variables in your data. I accomplished this by pivoting your data to a longer format.
library(tidyr)
df2 <- df %>% pivot_longer(-Maturity, names_to = "Yield", values_to = "Rate")
Then, I only used one geom_line and one geom_point, but with the new Yield variable mapped to group, fill, and shape. Now you can use scale_shape_manual and scale_fill_manual to set the appearances the way you want.
p = ggplot(data=df2, aes(x = Maturity, y = Rate, group = Yield,
fill = Yield, shape = Yield)) +
ggtitle("Curve of Zero-coupon Yield, Par Yield, Forward Yield")+
labs(x = "Maturity (year)", y = "Yield") +
scale_y_continuous(breaks=seq(100*(min(na.omit(Forward), c)-0.01),
100*(max(na.omit(Forward),c)+0.01),by=0.1)/100,
sec.axis = dup_axis(),
labels = scales::number_format(accuracy = 0.005)) +
scale_x_continuous("Maturity", labels=as.character(Maturity), breaks=Maturity,
sec.axis = dup_axis()) +
theme_classic() +
geom_line(size=1) +
geom_point(size=3, stroke=1.5, color="black") +
scale_shape_manual(values = c("ParYield" = 21, "R" = 22, "ForwardRate" = 23),
labels = c("Par Yield", "Zero-coupon Yield", "Forward Yield")) +
scale_fill_manual(values = c("ParYield" = rgb(69/255, 117/255, 180/255),
"R" = rgb(145/255, 191/255, 219/255),
"ForwardRate" = rgb(224/255, 243/255, 248/255)),
labels = c("Par Yield", "Zero-coupon Yield", "Forward Yield")) +
theme(plot.title = element_text(size=14, hjust=0.5),
text = element_text(size=15, colour = "black", family = "Calibri"),
axis.ticks.length = unit(-0.25, 'cm'),
axis.line = element_line(size=1),
axis.ticks = element_line(size=1),
axis.text.x = element_text(margin = margin(t=15)),
axis.text.x.top = element_text(margin = margin(b=15)),
axis.text.y.right = element_text(margin = margin(l=15, r=5)),
axis.text.y = element_text(margin = margin(l=5, r=15)),
axis.title.x.top = element_blank(),
axis.title.y.right = element_blank())+
guides(colour=guide_legend(override.aes = list(pch=c(16,21,20),fill=c('r','r','r'))))+
theme(legend.position = c(0.8,0.8), legend.justification = c("right", "top"))
I would recommend rethinking your y axis labels.'

ggplot2 - How to plot graphs side by side leaving only one legend

based on the sample dataset below, I Iam trying to make 2 graphs and place them side by side. I have tried using the cowplot package to do so. The final output was really messy.
Is there a way I could plot them side by side displaying only 1 legend, centralized on the bottom (since both graphs contain the same elements)?
df <- data.frame(Year = c(rep(2012,5), rep(2016,5),
rep(2012,5), rep(2016,5),
rep(2012,5), rep(2016,5)),
Category = rep(c('A1','A2','A3','A4','A5'),6),
Group = rep(c('T1','T1','T1','T1','T1',
'T2','T2','T2','T2','T2',
'T3','T3','T3','T3','T3'),2),
Variable = runif(30,0,100))
plot2012 <- ggplot(df, aes(x = Group, y = Variable)) +
geom_bar(aes(fill = Category), stat = 'identity') +
ylim(0,500) +
labs(y = 'Variable') +
theme(legend.position = 'bottom',
legend.title = element_blank(),
axis.title = element_text(size = 10),
legend.text = element_text(size = 10),
axis.text = element_text(size = 10))
plot2016 <- ggplot(df, aes(x = Group, y = Variable)) +
geom_bar(aes(fill = Category), stat = 'identity') +
ylim(0,500) +
labs(y = 'Variable') +
theme(legend.position = 'bottom',
legend.title = element_blank(),
axis.title = element_text(size = 10),
legend.text = element_text(size = 10),
axis.text = element_text(size = 10))
Any other general comments one the formatting/code are welcome
You can add facet_grid to your ggplot call.
ggplot(df, aes(x = Group, y = Variable)) +
geom_bar(aes(fill = Category), stat = 'identity') +
ylim(0,500) +
labs(y = 'Variable') +
# Generate plots as panel based on Year
facet_grid(. ~ Year) +
theme(legend.position = 'bottom',
legend.title = element_blank(),
axis.title = element_text(size = 10),
legend.text = element_text(size = 10),
axis.text = element_text(size = 10))

Resources