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"))
Related
I have 3 multi-plot figures I'm making for a manuscript and R is shrinking the first plot in the 2 figures with 3 paneled plots. They have different axes and scales so I couldn't use a facet function, but ggarrange() does what I need. Is there a way to make the size of the actual graphs identical?enter image description here
Plot 1 axes smaller
Here's the script generating them:
library(ggplot2)
library(ggpubr)
library(ggpmisc)
library(ggrepel)
library(tidyverse)
summary_bp <- read.table("1500bp_amas_summary.txt", header=TRUE)
alignment_length <- summary_bp$Alignment_length
no_inf_sites <- summary_bp$Parsimony_informative_sites
Length_Site_Reg <- ggplot(summary_bp, aes(x=alignment_length, y=no_inf_sites)) +
geom_point(shape = 1, size = 1) +
geom_smooth(method = lm, size = 0.7) +
stat_regline_equation(label.x = 3000, label.y = 3500, geom = "text", aes(label= ..rr.label..), size = 2) +
stat_cor(method = "pearson", label.x = 3000, label.y = 3100,aes(label = ..p.label..), size = 2) +
coord_cartesian(xlim = c(1500, 15000), ylim = c(0,4000)) +
scale_x_continuous(breaks = seq(1500, 15000, by=1500)) +
scale_y_continuous(breaks = seq(0, 4000, by=500)) +
labs( x = "Alignment Lengths", y = "Number of Parsimony \n Informative Sites") +
theme(aspect.ratio = 1, axis.text.x = element_text(angle=45, hjust = 1),
axis.line = element_line(color = "black"), panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_text(size = 7),
axis.title.x = element_text(size = 9, face = "bold"), axis.title.y = element_text(size = 8, face = "bold"))
Length_Site_Reg
#Fig. 3b) GC hist
summary_all <- read.table("summary_trim.txt", header=TRUE, sep = ",")
GC_hist <- ggplot(data = summary_all, aes(x=GC_content)) +
geom_histogram(binwidth = 0.025, fill = "white", color = "black") +
xlim(0.3,0.725) +
coord_cartesian( ylim = c(0,1050)) +
labs(x = "GC Content", y = "Number of Orthologs") +
theme(aspect.ratio = 1, panel.background = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), legend.position = c(0.75, 0.65),
axis.line = element_line(color = "black"), axis.text = element_text(size = 8),
axis.title = element_text(size = 9, face = "bold"), legend.key.size = unit(0.4, 'cm'),
legend.key.height = unit(0.4, 'cm'),
legend.key.width = unit(0.4, 'cm'),
legend.title = element_text(size=6),
legend.text = element_text(size=6)) +
geom_vline(aes(xintercept=mean(GC_content), color="mean"),
linetype="solid", size=0.5, show.legend = FALSE)
GC_hist
# Fig. 3c) K2P Divergence Hist
#trimmed_k2 <- file.choose()
trimmed_k2_data <- read.table("trimmed_alignment_k2.csv", header=TRUE, sep = ",")
trimmed_k2_data
trimmed_k2_data_hist <- ggplot(data = trimmed_k2_data, aes(x=distance)) +
geom_histogram(binwidth = 0.025, fill = "white", color = "black") +
coord_cartesian( ylim = c(0,1200) ) + scale_y_continuous(breaks = seq(0,1200,200)) +
labs(x = "K2P Distance", y = "Number of Orthologs", size= 3) +
theme(aspect.ratio = 1, panel.background = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(color = "black"),
axis.text = element_text(size = 8),
axis.title = element_text(size = 9, face = "bold")) +
geom_vline(aes(xintercept=mean(distance)), linetype="solid", colour="red", size=0.5, show.legend = FALSE)
trimmed_k2_data_hist
Fig3_plots <-ggarrange(Length_Site_Reg, GC_hist, trimmed_k2_data_hist, ncol = 3, widths = 1, heights = 1)
Fig3_plots
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
I want to increase the width of the sticks on my lollipop graph, and increase the size of the font of my y-labels. How would I go about doing that?
df_graph2 <- data.frame(
parameters = c("Cut back spending on food",
"Used up all or most saving",
'Increased credit card debt',
'Took money out of long-term savings',
'Borrowed money from family or friends',
'Pawned or sold possessions'),
values <- c(34.40,26.00,25.50,14.40,12.70,11.30))
df_graph2 %>%
ggplot() + aes(x=parameters, y=values) +
geom_segment( aes(x=parameters, xend=parameters, y=0, yend=values), color="gray82") +
geom_point( color="darkorange", size=4.2, alpha=0.9) +
geom_text(aes(label = paste(values,"%")), hjust = -.3,size=3.8,family="Arial") +
expand_limits(y = 100)+
theme_light() +
coord_flip() +
theme(
plot.margin = margin(1, 1, 4, 1.1, "cm"),
panel.grid.major.y = element_blank(),
axis.text.y = element_text(color = 'black'),
panel.border = element_blank(),
axis.ticks.y = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()
)
Change the y axis label size using the size parameter inside the element_text call of theme(axis.text.y and change the line width of the lollipops with size = inside geom_segment
ggplot(df_graph2, aes(parameters, values)) +
geom_segment(aes(xend = parameters, y = 0, yend = values),
size = 2, color = "gray82") +
geom_point(color = "darkorange", size = 4.2, alpha = 0.9) +
geom_text(aes(label = paste(values,"%")), hjust = -.3, size=3.8) +
expand_limits(y = 100) +
coord_flip() +
theme_void() +
theme(plot.margin = margin(1, 1, 4, 1.1, "cm"),
axis.text.y = element_text(color = 'black', size = 12, hjust = 1))
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
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)