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?
Related
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)))
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 can I shift the legend by several points (the width of the graph box line) to the left and bottom?
Task: I want to make the background of the legend semi-transparent, but so it doesn't overlap the graph box.
(red border - for better visualization of the problem)
Use the code:
image + theme(
panel.background = element_rect(fill = "white", color = NA),
panel.border = element_rect(fill = NA, color = "black", size = 2),
panel.grid.major = element_line(color = "#00000040", linetype = 3),
axis.text = element_text(size = 10),
axis.title = element_text(size = 12),
axis.title.x = element_text(margin = margin(t = 10)),
axis.title.y = element_text(margin = margin(r = 10)),
legend.key = element_rect(fill = NA, color = NA),
legend.background = element_rect(fill = "#ffffff80", color = "red", size = 1),
legend.justification = c(1, 1),
legend.position = c(1, 1),
legend.title = element_text(size = 10, color = "black"),
plot.title = element_text(hjust = 0.5),
)
If you want the legend box to align with the outside edge of the plot border, you need to adjust the legend.box.margin so that the top edge has the same value as the width of the line defined in legend.background.
There was no sample data, so I created some:
library(ggplot2)
x <- seq(0, 2 * pi, length.out = 100)
df <- data.frame(x = c(x, x), y = c(-cos(x), sin(x)),
group = rep(c("data1", "data2"), each = 100))
image <- ggplot(df, aes(x, y)) +
geom_line(aes(colour = group)) +
scale_colour_manual(values = c("red", "black"))
image + theme(
panel.background = element_rect(fill = "white", color = NA),
panel.border = element_rect(fill = NA, color = "black", size = 2),
panel.grid.major = element_line(color = "#00000040", linetype = 3),
axis.text = element_text(size = 10),
axis.title = element_text(size = 12),
axis.title.x = element_text(margin = margin(t = 10)),
axis.title.y = element_text(margin = margin(r = 10)),
legend.key = element_rect(fill = NA, color = NA),
legend.background = element_rect(fill = "#ffffff80", color = "red", size = 1),
legend.justification = c(1, 1),
legend.position = c(1, 1),
legend.title = element_text(size = 10, color = "black"),
legend.box.margin = margin(1, 0, 0, 0),
plot.title = element_text(hjust = 0.5)
)
Created on 2020-05-25 by the reprex package (v0.3.0)
found it!
legend.box.margin = margin(t = 2, r = 2),
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)
I'm experiencing a problem when using ggplot. my code shoud afaik give me a frame around the whole plot using (plot.background = element_rect(color="black"). However I only get 3 out of four lines. Any ideas on how to get 4 out of 4?
I have this theme:
theme.plot.bar <- function(){
theme(
text = element_text(family = "sans", face = "plain", colour = "black", size = 12, hjust = 0.5, vjust = 0.5, angle = 0, lineheight = 0.9),
axis.text.x = element_text(size = 13, colour = "black"),
axis.text.y = element_text(size = 11, colour = "black"),
strip.text = element_text(size = rel(0.8)),
axis.text.x = element_text(vjust = 1),
axis.text.y = element_text(hjust = 1),
axis.ticks = element_blank(),
axis.title.x = element_text(),
axis.title.y = element_text(angle = 90),
panel.background = element_rect(fill = NA ,colour = NA),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_line(colour = "black", linetype= "dotted", size =0.6),
strip.text.x = element_text(),
strip.text.y = element_text(angle = -90),
plot.background = element_rect(colour = "black", size = 0.5, linetype= 1, fill="white"), # Kant i højre side kommer ikke med hvis den sættes?
plot.margin = unit(c(0, 1, 0, 0), "cm"),
complete = FALSE)
}
Which gives me this output with ggplot:
EDIT: with plot.margin changed to 3 cm:
EDIT 2: with session info and ggplot-code as requested
NB: It still dont work when setting all plot margings as 2 inhes. I use png
sessionInfo()
R version 2.15.3 (2013-03-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
x <- factor(pol$job_komp_v2)
df <- as.data.frame(na.omit(x))
names(df) <- "v1"
farve <- rgb(144,26,30, maxColorValue =255)
r <- ggplot(df,aes(x=v1)) +
geom_bar(aes(y = (..count..)/sum(..count..)),
fill=farve, width=0.35) +
scale_y_continuous(labels = percent, "", limits=c(0,1), breaks= c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1)) +
scale_x_discrete(name= "") +
theme.plot.bar()
r