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

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)

Related

How to make a dual y-axis line plot with a polynomial function in ggplot2?

I need help with the modification of a plot with y-axis. In Excel I don't have trouble, but the hard part is in R. Besides, I uploaded an example of the final plot in excel.
It is not relevant the data you used.
I will appreciate your help.
Required double y-axis with polynomial function plot:
#Make lm model
Model<- lm(V~ T,data=data1)
summary(Model)
anova(Model)
HSD.test(Model, 'T', group = T, MSerror, alpha = 0.05, console = T)
#Make eq
eq <- function(data1){
m<- lm(V~T);
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(unname(coef(m)[1]), digits = 2),
b = format(unname(coef(m)[2]), digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
}
#Make plot
ggplot(data, aes(x=Time, y=mean))+
geom_line()+
geom_smooth(method=loess)+
stat_regline_equation(label.y = 4.0e-06, aes(label=..eq.label..))+
stat_regline_equation(label.y = 3.8e-06, aes(label=..rr.label..))+
geom_pointrange(aes(ymin=mean-se, ymax=mean+se))+
theme(
line = element_line(colour = "black", size = 1, linetype = 1, lineend = "butt"),
rect = element_rect(fill = "white", colour = "black", size = 1, linetype = 1),
aspect.ratio = 1,
plot.background = element_rect(fill = "white"),
plot.margin = margin(1, 1, 1, 1, "cm"),
axis.text = element_text(size = rel(2.5), colour = "#000000", margin = 1),
strip.text = element_text(size = rel(0.8)),
axis.line = element_blank(),
axis.text.x = element_text(vjust = 0.2, angle=0),
axis.text.y = element_text(hjust = 1),
axis.ticks = element_line(colour = "#000000", size = 1.2),
axis.title.x = element_text(size = 30, vjust=0.5),
axis.title.y = element_text(size = 30, angle = 90),
axis.ticks.length = unit(0.1, "cm"),
legend.background = element_rect(colour = NA),
legend.margin = unit(0.15, "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(2.0)),
legend.text.align = NULL,
legend.title = element_text(size = rel(2.0), face = "bold", hjust = 0),
legend.title.align = NULL,
legend.position = "right",
legend.direction = NULL,
legend.justification = "right",
legend.box = NULL,
panel.background = element_rect(fill = "#ffffff", colour = "#000000",
size = 2, linetype = "solid"),
panel.border = element_rect(colour = "black", fill=NA, size=2),
)+
ylab(expression(Volume~(m^3))) + xlab(expression(Time~(min)))

Percents in stacked bar chart

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

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

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"))

ggplot - add custom font to theme

I created an theme for ggplot, looks like this:
my_theme <- function() {
theme_minimal() %+replace%
theme(
plot.margin = unit(c(1, 1, 1, 0.5), "cm"),
plot.title = element_text(size = 14, face = "bold", hjust = 0.5, colour = "#DBD9D9", margin = margin(0,0,10,0)),
plot.subtitle = element_text(size = 12, hjust = 0.5, colour = "#797B86", margin=margin(0,0,30,0)),
plot.background = element_rect(fill = "#2D3142"),
panel.border = element_rect(colour = "#2D3142", fill=NA, size=1),
panel.grid.major.x = element_blank(),
panel.grid.major = element_line(colour = "#586082", size = 0.1, linetype = "dotted"),
panel.grid.minor = element_line(colour = "#586082", size = 0.3, linetype = "dotted"),
panel.background = element_rect(fill = '#2D3142'),
axis.line = element_line(colour = "#2D3142"),
axis.title = element_text(colour = "#797B86", size = 11),
axis.text = element_text(colour = "#DBD9D9", size = 9, face = "bold"),
axis.title.x = element_text(margin = margin(0.5, 0, 1, 0, unit = "cm")),
axis.title.y = element_text(margin = margin(0, 0.5, 0, 0, unit = "cm")),
legend.margin = margin(unit(c(3, 3, 3, 3), "cm")),
legend.position = "bottom",
legend.background = element_rect(fill = "#2D3142", color = NA),
legend.text = element_text(colour = "#DBD9D9", size = 11, face = "bold"),
legend.title = element_text(colour = "#797B86", size = 11),
legend.key = element_blank(),
complete = TRUE
)
}
This works well until I tried to add custom font, once I did it like this
with_font <- function() {
windowsFonts(lucida = windowsFont("Lucida Console"))
theme_minimal() %+replace%
theme(
text = element_text(family = "lucida"),
plot.margin = unit(c(1, 1, 1, 0.5), "cm"),
plot.title = element_text(size = 14, face = "bold", hjust = 0.5, colour = "#DBD9D9", margin = margin(0,0,10,0)),
plot.subtitle = element_text(size = 12, hjust = 0.5, colour = "#797B86", margin=margin(0,0,30,0)),
plot.background = element_rect(fill = "#2D3142"),
panel.border = element_rect(colour = "#2D3142", fill=NA, size=1),
panel.grid.major.x = element_blank(),
panel.grid.major = element_line(colour = "#586082", size = 0.1, linetype = "dotted"),
panel.grid.minor = element_line(colour = "#586082", size = 0.3, linetype = "dotted"),
panel.background = element_rect(fill = '#2D3142'),
axis.line = element_line(colour = "#2D3142"),
axis.title = element_text(colour = "#797B86", size = 11),
axis.text = element_text(colour = "#DBD9D9", size = 9, face = "bold"),
axis.title.x = element_text(margin = margin(0.5, 0, 1, 0, unit = "cm")),
axis.title.y = element_text(margin = margin(0, 0.5, 0, 0, unit = "cm")),
legend.margin = margin(unit(c(3, 3, 3, 3), "cm")),
legend.position = "bottom",
legend.background = element_rect(fill = "#2D3142", color = NA),
legend.text = element_text(colour = "#DBD9D9", size = 11, face = "bold"),
legend.title = element_text(colour = "#797B86", size = 11),
legend.key = element_blank(),
complete = TRUE
)
}
I always get an error
Error in FUN(X[[i]], ...) : Theme element 'text' has NULL property:
face, colour, size, hjust, vjust, angle, lineheight, margin, debug
Once i try to use it in my plot. Could you guys please suggest me whats wrong with that?

Resources