plot.background only shows 3 out of 4 lines - r

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

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

How to add basemaps to spatraster plot with ggplot 2?

I want to add this basemap using to raster using ggplot2 and terra packages and I am stuck.
Can I have some help figuring this out?
library(basemaps)
library(terra)
Library(rasterVis)
set_defaults(Yiel_total , map_service = "esri" , map_type = "world_hillshade")
x <- basemap_magic(Yiel_total , map_service = "esri" , map_type = "world_hillshade")
#Yiel_total was used for the extent
SpatRaster Data for Yiel_total
The ggplot code:
Yield <-rast("Yield_total")
Yield_Tot_6 <- gplot(Yield ) + geom_tile(aes(fill = value)) + ggtitle("")+
theme(panel.spacing.x=unit(0,"lines"), axis.title.x = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(),
axis.title.y = element_blank(),plot.title = element_text(hjust = 0.5, size = 18, face = "bold"),strip.background = element_blank(),
strip.text = element_text(size = 14, color = "black", face = "bold"),legend.text = element_text(size = 14, face = "bold"),
legend.title = element_text(hjust = 0.1, size = 16, face = "bold"), panel.background = element_blank()) +
guides(fill = guide_colourbar(title = "Relative yield",title.hjust = 0.5, barheight = 15, barwidth = 2))+
#basemap_magic(Yield_0.5_6, map_service = "esri" , map_type = "world_hillshade")+
facet_wrap(~ variable, ncol = 3 ) +
scale_fill_gradient(limits = c(0.01,0.88),low = 'red', high = 'dark green', na.value = "grey93", breaks = c(0.02,0.20, 0.40, 0.60,0.75, 0.87))
Yield_Tot_6
You can use tidyterra to work easily with ggplot2 and SpatRasters. I modified the call to basemap_raster and converted it also to SpatRaster:
library(basemaps)
library(terra)
Yiel_total <- rast("Yiel_total.tif")
set_defaults(Yiel_total, map_service = "esri", map_type = "world_hillshade")
x <- basemap_raster(Yiel_total, map_service = "esri", map_type = "world_hillshade")
x_terr <- rast(x)
library(ggplot2)
library(tidyterra)
ggplot() +
geom_spatraster_rgb(data = x_terr) +
geom_spatraster(data = Yiel_total) +
# Faceting with tidyterra
facet_wrap(~lyr, ncol = 3) +
scale_fill_gradient(
limits = c(0.01, 0.88), low = "red", high = "dark green",
na.value = NA, breaks = c(0.02, 0.20, 0.40, 0.60, 0.75, 0.87)
) +
ggtitle("") +
theme(
panel.spacing.x = unit(0, "lines"), axis.title.x = element_blank(),
axis.text = element_blank(), axis.ticks = element_blank(),
axis.title.y = element_blank(), plot.title = element_text(
hjust = 0.5,
size = 18, face = "bold"
), strip.background = element_blank(),
strip.text = element_text(size = 14, color = "black", face = "bold"),
legend.text = element_text(size = 14, face = "bold"),
legend.title = element_text(hjust = 0.1, size = 16, face = "bold"),
panel.background = element_blank()
) +
guides(fill = guide_colourbar(
title = "Relative yield", title.hjust = 0.5,
barheight = 15, barwidth = 2
))

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 properly resize all aspects of a ggplot in R, including the legend?

I'm making scatterplots in ggplot2 and then using ggsave to export PDFs of specific widths and heights. However, the figure legend never gets properly resized with ggsave (its borders don't stay within the plot). Is there another way to simultaneously resize all parts of a ggplot for easy exporting? I've also tried using pdf(), but the same problem occurs.
Here is an example of what's going on using iris:
data(iris)
test <- ggplot(iris, aes(x = iris$Sepal.Length, y = iris$Sepal.Width,
colour = iris$Species)) +
geom_point(size = .1) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black", size = .8),
legend.key=element_blank(),
axis.ticks = element_line(colour = "black", size = .6),
axis.text=element_text(size=6, colour = "black"),
plot.title = element_text(hjust = 0.0, size = 6, colour = "black"),
axis.title=element_text(size= 6),
legend.text=element_text(size = 6),
legend.title=element_text(size = 6, face = "bold"),
legend.position = c(.9, .15)) + labs(colour = "Species")
##saving plot with no resizing
ggsave(plot = test, file = "NoResize.pdf", dpi = 600, family = "ArialMT")
##saving plot with resizing results in relatively larger legend
ggsave(plot = test, file = "Resize.pdf", device = "pdf", width = 3.5,
height = 3, units = "in", dpi = 600, family = "ArialMT")
The legend seems to be changing, but it seems to get relatively bigger than the other aspects of the plot, such that it no longer fits within the plot axes.
The size argument is in points. If you have a preferred size for the default 7 inch by 7 inch graphic then you'll need to scale the size accordingly for a different size graphic.
Two other notes.
Don't use $ in the ggplot2::aes call. This will cause you problems in the future. It is sufficient to use ggplot(iris) + aes(x = Sepal.Length).
absolute legend.position in this example can be difficult to use in the different size graphics. I would recommend legend.position = "bottom" instead.
Here are two ways to control the relative size of the font in your example.
library(ggplot2)
data(iris)
# Okay solution, using a scaling value in an expression. I would not recommend
# this in general, but it will work. A better solution would be to use a
# function
test <-
expression({
ggplot(iris) +
aes(x = Sepal.Length, y = Sepal.Width, colour = Species) +
geom_point(size = .1) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black", size = .8),
legend.key = element_blank(),
axis.ticks = element_line(colour = "black", size = .6),
axis.text = element_text(size = 6 * scale_value, colour = "black"),
plot.title = element_text(hjust = 0.0, size = 6 * scale_value, colour = "black"),
axis.title = element_text(size = 6 * scale_value),
legend.text = element_text(size = 6 * scale_value),
legend.title = element_text(size = 6, face = "bold"),
legend.position = c(.9, .15)) +
labs(colour = "Species")
})
scale_value <- 1
ggsave(eval(test), width = 7 * scale_value, height = 7 * scale_value, file = "sotest1.pdf")
scale_value <- 3/7
ggsave(eval(test), width = 7 * scale_value, height = 7 * scale_value, file = "sotest2.pdf")
# Define a function to do the work.
iris_plot <- function(scale_value = 1, filename) {
g <- ggplot(iris) +
aes(x = Sepal.Length, y = Sepal.Width, colour = Species) +
geom_point(size = .1) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black", size = .8),
legend.key = element_blank(),
axis.ticks = element_line(colour = "black", size = .6),
axis.text = element_text(size = 6 * scale_value, colour = "black"),
plot.title = element_text(hjust = 0.0, size = 6 * scale_value, colour = "black"),
axis.title = element_text(size = 6 * scale_value),
legend.text = element_text(size = 6 * scale_value),
legend.title = element_text(size = 6, face = "bold"),
legend.position = c(.9, .15)) +
labs(colour = "Species")
ggsave(g, width = 7 * scale_value, height = 7 * scale_value, file = filename)
}
iris_plot(filename = "iris7x7.pdf")
iris_plot(4/7, filename = "iris4x4.pdf")
EDIT
Using the package magick will give you a nice programming interface for resizing and editing graphics via imagemagick
For example
library(ggplot2)
library(magick)
g <-
ggplot(iris) +
aes(x = Sepal.Length, y = Sepal.Width, colour = Species) +
geom_point(size = .1) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black", size = .8),
legend.key = element_blank(),
axis.ticks = element_line(colour = "black", size = .6),
axis.text = element_text(size = 6, colour = "black"),
plot.title = element_text(hjust = 0.0, size = 6, colour = "black"),
axis.title = element_text(size = 6),
legend.text = element_text(size = 6),
legend.title = element_text(size = 6, face = "bold"),
legend.position = c(.9, .15)) +
labs(colour = "Species")
ggsave(g, file = "iris7x7.pdf", width = 7, height = 7)
iris_g <- image_read("iris7x7.pdf")
iris_3x3 <- image_scale(iris_g, "216x216")
image_write(iris_3x3, path = "iris3x3.pdf", format = "pdf")
Note, the resized graphic may require some edits to deal with pixelation or blurriness.
Again, I would recommend against using an absolute legend.position value. Perhaps, if you know you need a 3in by 3in graphic you can open a dev window with those dimensions to build your graphic in and then save appropriately. For example, open a 3in by 3in X Window via X11(width = 3, height = 3).

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