Related
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
I'm trying to paste 16 different plots into one with a common x axis
I successfully made, plot and paste the graphs individually with grid.draw()
But i haven't been able to transform the x axis in a continuous way or at least remove the space between each graph
i attach some code to illustrate how the graphs were made.
plot1 = ggplot(map_snp %>% filter( chr == chr[1] ), aes(x=POS)) +
geom_histogram( binwidth = 2,
col=palette[2],
fill=palette[2],
alpha = .2) +
xlab("Chromosome 1") +
ylab("SNP count") +
theme_bw() +
theme(axis.text.x = element_text(angle = 0, colour = "black"),
text=element_text(family="Times New Roman", size = 12),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_line(colour = "black"),
axis.line.x = element_line(colour = "black"))
par_plots <- list()
for (i in list_par) {
par_plots[[i]] = ggplot(map_snp %>% filter( chr == i ), aes(x=POS)) +
geom_histogram( binwidth = 2,
col= palette[3],
fill= palette[3],
alpha = .2) +
xlab(paste0(i)) +
theme_bw() +
theme(axis.text.x = element_text(angle = 0, colour = "black"),
text=element_text(family="Times New Roman", size = 12),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.x = element_line(colour = "black"))
}
impar_plots <- list()
for (i in list_impar) {
impar_plots[[i]] = ggplot(map_snp %>% filter( chr == i ), aes(x=POS)) +
geom_histogram( binwidth = 2,
col= palette[2],
fill=palette[2],
alpha = .2) +
xlab(paste0(i)) +
theme_bw() +
theme(axis.text.x = element_text(angle = 0, colour = "black"),
text=element_text(family="Times New Roman", size = 12),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.x = element_line(colour = "black"))
}
grid.newpage()
png(paste("./snp_map.png"), width = 15*4.5, height = 8, res = 320, units = "cm", pointsize = 12, bg = "white")
grid.draw(cbind(ggplotGrob(plot1),
ggplotGrob(par_plots$`Chromosome 2`),
ggplotGrob(impar_plots$`Chromosome 3`),
ggplotGrob(par_plots$`Chromosome 4`),
ggplotGrob(impar_plots$`Chromosome 5`),
ggplotGrob(par_plots$`Chromosome 6`),
ggplotGrob(impar_plots$`Chromosome 7`),
ggplotGrob(par_plots$`Chromosome 8`),
ggplotGrob(impar_plots$`Chromosome 9`),
ggplotGrob(par_plots$`Chromosome 10`),
ggplotGrob(impar_plots$`Chromosome 11`),
ggplotGrob(par_plots$`Chromosome 12`),
ggplotGrob(impar_plots$`Chromosome 13`),
ggplotGrob(par_plots$`Chromosome 14`),
ggplotGrob(impar_plots$`Chromosome 15`),
ggplotGrob(par_plots$`Chromosome 16`),
size = "last"))
dev.off()
SNP MAP
I can get rid of most of the border, but not quiiiiite all. Here using an example from built in diamonds data set from ggplot2.
par_plots <- list()
cuts = unique(diamonds$cut)
for (c in as.numeric(cuts)) {
par_plots[[c]] = ggplot(diamonds %>% filter(as.numeric(cut) == c),
aes(x = price)) +
geom_histogram() +
scale_x_continuous(expand = expansion(0)) +
xlab(paste0(i)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 0, colour = "black"),
text=element_text(family="Times New Roman", size = 12),
plot.margin = unit(c(0,0,0,0), "cm"),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.x = element_line(colour = "black"))
}
library(grid)
grid.newpage()
# png(paste("./snp_map.png"), width = 15*4.5, height = 8, res = 320, units = "cm", pointsize = 12, bg = "white")
grid.draw(cbind(ggplotGrob(par_plots[[1]]),
ggplotGrob(par_plots[[2]]),
ggplotGrob(par_plots[[3]]),
ggplotGrob(par_plots[[4]])))
dev.off()
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
I want to place ticks on all four sides of my graph. The way suggested for this is mirror_ticks.
library(ggplot2)
library(ggplotTicks)
sp6<-ggplot(Anna_Smooth, aes(y=log10(Prob2), x=log10(AvSize)))+
geom_point( data=Anna_Smooth, aes(y=log10(Prob2), x=log10(AvSize), color=PART) )+
guides( color=FALSE)
sp8<-sp6+ labs(x=expression(paste(log(s))))+
labs(y=expression(paste(log(P(s)))) )+
theme(axis.text.y = element_text(size=14),
axis.text.x = element_text(size=14),
axis.title.y = element_text(size=15),
axis.title.x = element_text(size=15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=2)
)
sp10<-mirror_ticks(sp10, allPanels=TRUE)
My output sp10 has no ticks on opposite panels, same result if I put allPanels=TRUE
Is there a fix? I am open to learn how one does this with theme settings?
As of ggplot2 version 2.2.0 (2016-11-11), the scale_x_continuous() and scale_y_continuous() can display a secondary axis which is positioned opposite to the primary axis and which can be controlled with the sec.axis argument.
This can be used to mirror the tick marks.
The OP hasn't provided reproducible data so we use the mpg dataset which comes with the ggplot2 package:
Chart without mirrored tick marks
library(ggplot2)
g1 <- ggplot(mpg, aes(log10(displ), log10(hwy))) +
geom_point() +
theme(
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(size = 15),
axis.title.x = element_text(size = 15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(
colour = "black",
fill = NA,
size = 2
)
)
g1
Chart with secondary axes
g1 +
scale_x_continuous(sec.axis = dup_axis()) +
scale_y_continuous(sec.axis = dup_axis())
g1 +
scale_x_continuous(sec.axis = dup_axis(name = NULL)) +
scale_y_continuous(sec.axis = dup_axis(name = NULL))
g1 +
scale_x_continuous(sec.axis = dup_axis(name = NULL, labels = NULL)) +
scale_y_continuous(sec.axis = dup_axis(name = NULL, labels = NULL))
Mirrored tick marks with log10 scales
The secondary axes are also available with the scale_x_log10() and scale_x_log10() functions.
So, it can be avoided to use the log() function within the call to aes() but by specifying an appropriate log scale:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme(
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(size = 15),
axis.title.x = element_text(size = 15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(
colour = "black",
fill = NA,
size = 2
)
) +
scale_x_log10(sec.axis = dup_axis(name = NULL, labels = NULL)) +
scale_y_log10(sec.axis = dup_axis(name = NULL, labels = NULL))
I've had trouble increasing the radius of the circle on a map. I've tried the scale_size_continuous with maximum size up to 1000, but the circle size remained the same after all.
My code is in the following:
states <- map_data("state")
gg1 <- ggplot(data=states) +
geom_polygon(data=states, aes(x=long,y=lat,group=group),
fill = "orange", color = "white") +
coord_fixed(ratio = 1.3)+
geom_circle(data = origCity, aes(x0 = Long, y0 = Lat, r = Percent),
fill = "black", color = "black", alpha = 0.5, show.legend = FALSE) +
scale_size_continuous(range = c(20, 5))+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())+
theme(axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank())+
theme(axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank())
print(gg1)
Any suggestions are greatly appreciated!