Percents in stacked bar chart - r

I'm trying to use percents as labels in a stacked bar chart, but when I convert the column to percents instead of decimals the bar chart automatically becomes vertical and no longer stacked. I would appreciate some helping making these labels percents instead of decimals.
Thanks.
Here is my sample code:
Form <- c(rep("Overall" , 4) , rep("x" , 4) , rep("y" , 4) , rep("z" , 4) )
Ease <- rep(c("Very Easy", "Somewhat Easy", "Somewhat Difficult", "Very Difficult") , 1)
Mean <- c(.28, .5, .19, .44, .33, .48, .15,.4, .32, .51, .15, .2, .30, .50, .16, .4)
data <- data.frame(Form,Ease,Mean)
ggplot(data, aes(x=Mean, y=Form, fill=Ease, label = Mean))+
geom_bar(position = "stack", stat = "identity", width = .4,
size=.2) +
geom_text(aes(x=Mean, y=Form, label = Mean),
position = position_stack(vjust = .5),
size = 4, color = "white", fontface = "bold")+
theme_fivethirtyeight()+
theme( legend.position = "bottom",
legend.key.width = unit(.7, "cm"),
legend.title = element_text(family="serif", size=8, color = "black", face = "bold"),
legend.text = element_text(family="serif", size=8, color = "black", face = "bold"),
legend.key.size = unit(.1, "cm"),
axis.line = element_line(size = 1),
axis.title = element_text(family="serif", size=18, color = "black", face = "bold"),
axis.text = element_blank(),
axis.ticks = element_blank(),
plot.background = element_blank(),
panel.background = element_blank(),
plot.title = element_text(hjust = 0.5, family="serif", size=22, color = "black", face = "bold"),
strip.text.x = element_text(family="serif", size=12, color = "black", face = "bold")) +
scale_fill_brewer(palette = "Set2")+
xlab("")+
ylab("")

Using this
ggplot(data, aes(x=Mean, y=Form, fill=Ease, label = Mean))+
geom_bar(position = "stack", stat = "identity", width = .4,
size=.2) +
geom_text(aes(x=Mean, y=Form, label = scales::percent(Mean)),
position = position_stack(vjust = .5),
size = 4, color = "white", fontface = "bold")+
#theme_fivethirtyeight()+
theme( legend.position = "bottom",
legend.key.width = unit(.7, "cm"),
legend.title = element_text(family="serif", size=12, color = "black", face = "bold"),
legend.text = element_text(family="serif", size=8, color = "black", face = "bold"),
legend.key.size = unit(.1, "cm"),
axis.line = element_line(size = 1),
axis.title = element_text(family="serif", size=18, color = "black", face = "bold"),
axis.text = element_blank(),
axis.ticks = element_blank(),
plot.background = element_blank(),
panel.background = element_blank(),
plot.title = element_text(hjust = 0.5, family="serif", size=22, color = "black", face = "bold"),
strip.text.x = element_text(family="serif", size=12, color = "black", face = "bold")) +
scale_fill_brewer(palette = "Set2")+
xlab("")+
ylab("")
we get
It is no different than your original output

Related

ggarrange - change background of shared legend and remove borders between plots

I would like the background to all be navy blue without the white borders around the plots and the legend background to also be navy blue (no large white box). I have seen some solutions here but they seem needlessly complicated and would involve a lot of rewriting of the script or even switching packages. Surely there is an easier way? Thanks in advance.
Example script:
df <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
cols <- c("VC" = "#0FC9F7", "OJ" = "#1010EB")
p1 <- ggplot(df, aes(x=dose, y=len, color=factor(supp))) +
geom_line(aes(group = supp)) +
theme(
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "#140F4B",
colour = "#140F4B",
size = 0.5, linetype = "solid"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "#140F4B"),
text = element_text(colour = "white"),
axis.line = element_line(colour = "white"),
axis.text = element_text(colour = "white")
) +
scale_color_manual(values = cols,
name = "supp",
breaks=c("VC", "OJ"),
labels=c("VC", "OJ"))
p2 <- ggplot(df, aes(x=dose, y=len, color=factor(supp))) +
geom_line(aes(group = supp)) +
theme(
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "#140F4B",
colour = "#140F4B",
size = 0.5, linetype = "solid"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "#140F4B"),
text = element_text(colour = "white"),
axis.line = element_line(colour = "white"),
axis.text = element_text(colour = "white")
) +
scale_color_manual(values = cols,
name = "supp",
breaks=c("VC", "OJ"),
labels=c("VC", "OJ"))
plots <- list(p1, p2)
combined_plots <- ggpubr::ggarrange(plotlist = plots,
ncol = 2, nrow = 1, align = "hv", common.legend = TRUE, legend = "bottom")
#Annotate plot
combined_plots <- annotate_figure(
combined_plots,
top = text_grob(paste0("Example"), size = 18, color = "white")
) + bgcolor("#140F4B")+ border("#140F4B")
It looks like the problem is that there is a missing legend.background in your theme which might explain why there is a white background around your legend. I managed to solve the problem using patchwork! Hopefully, this helps.
library(ggplot2)
library(patchwork)
df <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
cols <- c("VC" = "#0FC9F7", "OJ" = "#1010EB")
p1 <- ggplot(df, aes(x=dose, y=len, color=factor(supp))) +
geom_line(aes(group = supp)) +
theme(
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "#140F4B",
colour = "#140F4B",
size = 0.5, linetype = "solid"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "#140F4B"),
text = element_text(colour = "white"),
axis.line = element_line(colour = "white"),
axis.text = element_text(colour = "white")
) +
scale_color_manual(values = cols,
name = "supp",
breaks=c("VC", "OJ"),
labels=c("VC", "OJ"))
p2 <- ggplot(df, aes(x=dose, y=len, color=factor(supp))) +
geom_line(aes(group = supp)) +
theme(
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "#140F4B",
colour = "#140F4B",
size = 0.5, linetype = "solid"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "#140F4B"),
text = element_text(colour = "white"),
axis.line = element_line(colour = "white"),
axis.text = element_text(colour = "white")
) +
scale_color_manual(values = cols,
name = "supp",
breaks=c("VC", "OJ"),
labels=c("VC", "OJ"))
combined <- p1 + p2 + plot_annotation(title = "Title Here") +
plot_layout(guides = "collect") &
theme(plot.title = element_text(colour = "white", size = 18), legend.position = "bottom",
legend.background = element_rect(fill = "#140F4B", colour = "#140F4B" ),
legend.key = element_rect(fill = "#140F4B", colour = "#140F4B" ),
plot.background = element_rect(fill = "#140F4B", colour = "#140F4B"))
combined

Unable to change color of line and scatter plot R ggplot

I am relatively new to ggplot plot so I think some of the intricacies are lost on me. I have plotted multiple months of data where the data is binned by the hour. Each line is meant to be colored by month where the x-axis is the hour of the day. I am having trouble changing the color of the lines and moved things around in ggplot to try to get it to work but the color of all lines remain black"
Here is an example of some of the data I am plotting: Example data
Here is my code:
p <- ggplot(mtozoneavgk_month, aes(hour, Avgk, group = factor(Date) )) +
geom_point(size = 4) +
geom_line(size = 1)+
scale_color_manual(values = c("#DC143C", "#B22222", "#000080", "#00008B",
"#0000CD", "#0000FF", "#66B2FF", "#FF6347", "#FF0000", "#B22222"),
name = "Month", labels = c("Sept-2019", "Oct-2019", "Nov-2019", "Dec-2019",
"Jan-2020", "Feb-2020", "Mar-2020", "April-2020", "May-2020", "Jun-2020"),
expand = c(0, 0))+
ylab("rate constant (k)")+
scale_y_continuous(label=scientific_10)+
#scale_y_continuous(labels = fancy_scientific)+
theme(axis.ticks.length = unit(0.2, "cm"),
axis.ticks = element_line(size = 2),
axis.text=element_text(size=12, face = 'bold'),
axis.title=element_text(size=14,face="bold"),
axis.text.x = element_text(color = "black", face = "bold", size = 14),
axis.text.y = element_text(color = "black", size = 14),
legend.title=element_text(size=14, face = "bold"),
legend.text = element_text(size = 14, face = "bold"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=3))
p + scale_x_continuous(breaks = c(0,2,4,6,8,10,12,14,16,18,20,22,24),
label = c(0,2,4,6,8,10,12,14,16,18,20,22,24),
expand = c(0, 0))
Any help would be appreciated!
If we simplify your code as follows (letting ggplot2 to take care of colors and labels):
p <- ggplot(mtozoneavgk_month, aes(hour, Avgk, group = factor(Date), color = factor(Date))) +
geom_point(size = 4) +
geom_line(size = 1)+
ylab("rate constant (k)")+
theme(axis.ticks.length = unit(0.2, "cm"),
axis.ticks = element_line(size = 2),
axis.text=element_text(size=12, face = 'bold'),
axis.title=element_text(size=14,face="bold"),
axis.text.x = element_text(color = "black", face = "bold", size = 14),
axis.text.y = element_text(color = "black", size = 14),
legend.title=element_text(size=14, face = "bold"),
legend.text = element_text(size = 14, face = "bold"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=3))
We obtain the following plot:
If you also want to control colors I would suggest to use named vectors:
pcolor <- c("#DC143C", "#B22222", "#000080", "#00008B",
"#0000CD", "#0000FF", "#66B2FF", "#FF6347", "#FF0000")
names(pcolor) <- unique(mtozoneavgk_month$Date)
plabel <- c("Sept-2019", "Oct-2019", "Nov-2019",
"Jan-2020", "Feb-2020", "Mar-2020", "April-2020", "May-2020", "Jun-2020")
names(plabel) <- unique(mtozoneavgk_month$Date)
p <- ggplot(mtozoneavgk_month, aes(hour, Avgk, group = factor(Date), color = factor(Date))) +
geom_point(size = 4) +
geom_line(size = 1)+
scale_color_manual(values = pcolor,
name = "Month",
labels = plabel,
expand = c(0, 0))+
ylab("rate constant (k)")+
theme(axis.ticks.length = unit(0.2, "cm"),
axis.ticks = element_line(size = 2),
axis.text=element_text(size=12, face = 'bold'),
axis.title=element_text(size=14,face="bold"),
axis.text.x = element_text(color = "black", face = "bold", size = 14),
axis.text.y = element_text(color = "black", size = 14),
legend.title=element_text(size=14, face = "bold"),
legend.text = element_text(size = 14, face = "bold"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=3))
Which results in:
Remark: I do not know if it was because of the sample you gave us but December 2019 is missing so tweaked a little bit your code. Be aware of it when you make your own

How do I change a dodged bar graph so the bars follow a selected color pattern,not a gradient, and how to keep bars from changing positions?

csv file I'm working with
***I'm relatively new to R and coding in general. I have done a bit of Googling and trial and error to figure out my mistakes/solve my issues but to no avail. Help is greatly appreciated.
For the file above, I'm creating a dodged bar graph to show each quarter from 2017-2020. However, it produces a gradient and I want individual colors. For example, I want all Q1s to be red. All Q2s to be green, etc. I tried using scale_fill_manual() but had no luck.
gradient result
testplot1<-ggplot(test, aes(x=Year, y=Sales, fill=Sales))+
geom_bar(position = "dodge", stat="identity", aes(group=Q))+
labs(x = "Year", y = "Sales",
title="Sales Year-Over-Year",
subtitles = "Test")+
theme_bw()+
theme_minimal()+
theme(axis.ticks = element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank(),
plot.title= element_text(face="bold", size=14, color = "black", hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"))+
theme(legend.position = "none")+
geom_text(aes(label=dollar(Sales)), hjust = 0.5, vjust = -0.8, color = "black",
size = 2,position = position_dodge2(width = 1),inherit.aes = TRUE)+
scale_y_continuous(labels=dollar_format(prefix="$"), expand = c(0,0), limits = c(-200000,800000))
testplot1
I tried setting the fill to as.factor(Sales), but then it changes the order of the 2020 quarters. Any help is appreciated.
various colors and rearranged 2020 quarters
testplot<-ggplot(test, aes(x=Year, y=Sales, fill=as.factor(Sales)))+
geom_bar(position = "dodge", stat="identity", aes(group=Q))+
labs(x = "Year", y = "Sales",
title="Sales Year-Over-Year",
subtitles = "Test")+
theme_bw()+
theme_minimal()+
theme(axis.ticks = element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank(),
plot.title= element_text(face="bold", size=14, color = "black", hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"))+
theme(legend.position = "none")+
geom_text(aes(label=dollar(Sales)), hjust = 0.5, vjust = -0.8, color = "black",
size = 2,position = position_dodge2(width = 1),inherit.aes = TRUE)+
scale_y_continuous(labels=dollar_format(prefix="$"), expand = c(0,0), limits = c(-200000,800000))
testplot
It can be tricky when you're trying to color a variable that you know is a categorical variable (the quarter) but that is listed as a number and thus that R interprets as a continuous variable. You can get around that by coding it as a factor like this:
ggplot(test, aes(x=Year, y=Sales, fill=as.factor(Q)))+
geom_bar(position = "dodge", stat="identity") +
labs(x = "Year", y = "Sales",
title="Sales Year-Over-Year",
subtitles = "Test") +
scale_fill_manual(values = c("red", "green", "blue", "gray")) +
theme_bw() +
theme_minimal() +
theme(axis.ticks = element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank(),
plot.title= element_text(face="bold", size=14, color = "black", hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"))+
theme(legend.position = "none")+
geom_text(aes(label=dollar(Sales)), hjust = 0.5, vjust = -0.8, color = "black",
size = 2, position = position_dodge2(width = 1),
inherit.aes = TRUE) +
scale_y_continuous(labels=dollar_format(prefix="$"),
expand = c(0,0), limits = c(-200000,800000))
The critical bit is the fill = as.factor(Q).

Position_dodge with preserve='single' do not align geom_text

Any ideas on how to solve the missialingment of text summary?
why position_dodge works well for error_bar but not for text??
I have also tried position_dodge2. It also gives lots of problems with error_bar positions....
ggplot(data=data_areas[data_areas$TIMEPOINT=='0h',], aes(ISOFORM,NEURITE_CELL_AREA_RATIOP)) +
geom_bar(aes(fill=CELLTYPE,color=CELLTYPE), position=position_dodge(width=0.9, preserve = "single"), width=0.8,stat = 'summary', fun.y = 'mean') +
geom_errorbar(aes(color = CELLTYPE), stat = 'summary',position=position_dodge(width=0.9, preserve = "single"), width = 0.2,size=2, fun.data = "mean_sdl", fun.args = list(mult = 1)) + # mult indica el numero de desviaciones estandar
#geom_point(aes(group=CELLTYPE), shape = 21, position = position_dodge(.8), size=2, fill="gray")+
scale_x_discrete(expand = c(0.5,0.05))+
scale_y_continuous(expand = c(0.05,0), limits = c(0, 25), breaks = seq(0, 25, by = 5))+
scale_fill_manual(name="Celltype",values =c("#7d7973","#7d7973","#7d7973","#7d7973","#7d7973","#7d7973","#7d7973"))+
scale_color_manual(name="Celltype",values =c("#7d7973","#7d7973","#7d7973","#7d7973","#7d7973","#7d7973","#7d7973"))+
stat_summary(aes(label=round(..y..,2),group=CELLTYPE), fun.y=mean, geom="text", size=4,color="red",position=position_dodge(width=0.9, preserve='single'), vjust = 2)+
theme(axis.title.x = element_blank(),
axis.title.y =element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
plot.margin = margin(1, 1, 1, 1, "cm"),
axis.line.y = element_line(colour = "black", size=1),
axis.line.x = element_line(colour = "black", size=1),
axis.ticks = element_line(color = "black", size = 0.5),
axis.ticks.length = unit(10, "pt"),
axis.ticks.x = element_blank(),
legend.position="none",
panel.background = element_rect(fill = 'white'),
panel.grid.major = element_line(colour = "#f0f0f0"))

ggplot2 - Reset axis text after it's been set to element_blank

Once someone has set the axis.text to element_blank(), can the original setting be easily recovered. By original, I mean the ggplot2 original, not the original as of the moment that element_blank() was set. Basically, can is there something like axis.text.x = element_default()?
library(ggplot2)
# A while ago in my code, someone might do this
x = ggplot(PlantGrowth, aes(x=group, y=weight)) +
geom_boxplot() +
theme(axis.text.x = element_blank())
# Is there an easy way for me to do this:
x2 = x + theme(axis.text.x = default_ggplot2_value_for_this_item)
ggplot2's default theme is theme_grey. Here are all the defaults for that:
line = element_line(colour = "black", size = 0.5, linetype = 1, lineend = "butt"),
rect = element_rect(fill = "white", colour = "black", size = 0.5, linetype = 1),
text = element_text(family = base_family,
face = "plain", colour = "black", size = base_size, hjust = 0.5, vjust = 0.5, angle = 0, lineheight = 0.9),
axis.text = element_text(size = rel(0.8), colour = "grey50"),
strip.text = element_text(size = rel(0.8)),
axis.line = element_blank(),
axis.text.x = element_text(vjust = 1),
axis.text.y = element_text(hjust = 1),
axis.ticks = element_line(colour = "grey50"),
axis.title.x = element_text(),
axis.title.y = element_text(angle = 90),
axis.ticks.length = unit(0.15, "cm"),
axis.ticks.margin = unit(0.1, "cm"),
legend.background = element_rect(colour = NA),
legend.margin = unit(0.2, "cm"),
legend.key = element_rect(fill = "grey95", colour = "white"),
legend.key.size = unit(1.2, "lines"),
legend.key.height = NULL,
legend.key.width = NULL,
legend.text = element_text(size = rel(0.8)),
legend.text.align = NULL,
legend.title = element_text(size = rel(0.8), face = "bold", hjust = 0),
legend.title.align = NULL,
legend.position = "right",
legend.direction = NULL,
legend.justification = "center",
legend.box = NULL,
panel.background = element_rect(fill = "grey90", colour = NA),
panel.border = element_blank(),
panel.grid.major = element_line(colour = "white"),
panel.grid.minor = element_line(colour = "grey95", size = 0.25),
panel.margin = unit(0.25, "lines"),
panel.margin.x = NULL,
panel.margin.y = NULL,
strip.background = element_rect(fill = "grey80", colour = NA),
strip.text.x = element_text(),
strip.text.y = element_text(angle = -90),
plot.background = element_rect(colour = "white"),
plot.title = element_text(size = rel(1.2)),
plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"), complete = TRUE)

Resources