I have created a map that shows that shows the intensity of a variable by making a grid of the entire United states. That works pretty well but the problem that a grid appears when I save the plot. The following simplified code reproduces my problem.
library(ggplot2)
library(scales)
library(gridExtra)
#Construct grid
n.dots <- 1000
x <- seq(0, 100, length.out = n.dots)
y <- seq(0, 100, length.out = n.dots)
grid <- expand.grid(x, y)
colnames(grid) <- c("x", "y")
grid$value <- grid$x*grid$y
#Make graph
mytheme <- theme(axis.text = element_blank(),
axis.title = element_blank(),
plot.margin=unit(c(0,0,0,0), "lines"),
axis.ticks = element_blank(),
plot.title = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
legend.title=element_blank(),
legend.key = element_rect(fill = "white", colour = "white"), text=element_text(size=10),
legend.position="none")
p <- ggplot() + geom_point(data=grid, aes(x=x, y=y, colour = value), size = 0.1) + scale_colour_gradient("value", low="green", high = "red") + theme_bw() + mytheme
ggsave(p, filename = "Map intensity.png", width = 4, height = 4, units = "in", dpi = 600)
The png file shows a grid which I do not want. By commenting out axis.text and axis.title in theme options I selected the problem disappears.
#Make graph
mytheme <- theme(#axis.text = element_blank(),
#axis.title = element_blank(),
plot.margin=unit(c(0,0,0,0), "lines"),
axis.ticks = element_blank(),
plot.title = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
legend.title=element_blank(),
legend.key = element_rect(fill = "white", colour = "white"), text=element_text(size=10),
legend.position="none")
p <- ggplot() + geom_point(data=grid, aes(x=x, y=y, colour = value), size = 0.1) + scale_colour_gradient("value", low="green", high = "red") + theme_bw() + mytheme
ggsave(p, filename = "Map intensity_2.png", width = 4, height = 4, units = "in", dpi = 600)
In that last png file the graph is all smooth as I want it and there is no grid. But it contains axis text and axis titles, which I do not want. What is causing this behavior?
One solution could be to increase the point size but I am hoping to keep the graph as precise as possible. I am interested in knowing why the axis options change the appearance of a grid in the file that is saved.
Thank you.
Related
This question already has answers here:
Place a border around points
(5 answers)
Closed 1 year ago.
I have a dataset like this:
Year<-rep(2001:2005, each = 5)
name<-c("John","Ellen","Mark","Randy","Luisa")
Name<-c(rep(name,5))
Value<-sample(seq(0,25,by=1),25)
mydata<-data.frame(Year,Name,Value)
And my plot looks like this:
p <- ggplot(mydata, aes(x=Year, y=reorder(Name, desc(Name)), size = Value)) +
geom_point(aes(colour = Value,
alpha = I(as.numeric(Value > 0))))
p <- p + scale_colour_viridis_c(option = "D", direction = -1,
limits = c(1, 25)) +
scale_size_area(guide = "none") +
ylab("Name") +
theme(axis.line = element_blank(),
axis.text.x=element_text(size=11,margin=margin(b=10),colour="black"),
axis.text.y=element_text(size=13,margin=margin(l=10),colour="black",
face="italic"),
axis.ticks = element_blank(),
axis.title=element_text(size=18,face="bold"),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.text = element_text(size=14),
legend.title = element_text(size=18))
I would like to improve it in two ways but I couldn't figure out how.
I would like to add a black border around points. I know I should use pch>20 and specify colour, but because my colours are mapped to a feature of the dataset (they depend on value, in this case), I don't know exactly how to do that. Note that value = 0 points are not plotted. Easy stratagems such as plotting bigger black points under my points seem utopic for me.
I would like to change the breaks of the scale (e.g., instead of having breaks every 5, I'd like to have breaks every 2.5), but it is a continuous scale, and I'm not sure how to do that.
I am not very familiar with ggplo2, thus any help would be appreciated!
You can indeed use a shape >20, e.g. I use shape=21 here. Then you need to change your scale_color_ to scale_fill_, because the color is now black (it is the border of the shape).
For breaks, you could just specify them in the scale itself. Combining both:
ggplot(mydata, aes(x=Year, y=reorder(Name, desc(Name)), size = Value)) +
geom_point(aes(fill = Value,
alpha = I(as.numeric(Value > 0))), shape=21, color = "black") +
scale_fill_viridis_c(option = "D", direction = -1,
limits = c(1, 25), breaks=seq(1, 25, 2.5)) +
scale_size_area(guide = "none") +
ylab("Name") +
theme(axis.line = element_blank(),
axis.text.x=element_text(size=11,margin=margin(b=10),colour="black"),
axis.text.y=element_text(size=13,margin=margin(l=10),colour="black",
face="italic"),
axis.ticks = element_blank(),
axis.title=element_text(size=18,face="bold"),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.text = element_text(size=14),
legend.title = element_text(size=18))
I have many subsets of data that I would like to apply functions to and then plot. I have tailored my ggplot code to be what I'd like, aesthetically, and don't want to fill my notebook with these same long lists of specifications for every independent plot. Is it possible to take something like the following blocks of ggplot text, where
p = [some generic ggplot]
p = p + guides(colour = guide_legend(title = "year_collected", ncol = 2, keywidth = 1, keyheight = 1))
p = p + theme_minimal() +
theme(panel.grid.major = element_blank(),
axis.title.y=element_text(margin=margin(0,20,0,0)),
text=element_text(size=8, color="black",family="Arial"),
panel.grid.minor = element_blank(),
#axis.ticks = element_blank(),
legend.key.size = unit(.1, "in"),
legend.spacing.x = unit(.01,"in"),
legend.position = "bottom",
plot.title = element_text(hjust = 0.5))
p$layers <- p$layers[-1]
and use the R function options to capture all of these specifications and additions?
Thanks!
Alternatively, you can feed any lines of ggplot code into a list (with items comma separated) and add that:
my_theme_lines <- list(theme_minimal(),
theme(panel.grid.major = element_blank(),
axis.title.y=element_text(margin=margin(0,20,0,0)),
text=element_text(size=8, color="black",family="Arial"),
panel.grid.minor = element_blank(),
#axis.ticks = element_blank(),
legend.key.size = unit(.1, "in"),
legend.spacing.x = unit(.01,"in"),
legend.position = "bottom",
plot.title = element_text(hjust = 0.5)))
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
my_theme_lines
It's easy to define a custom ggplot theme, just use this template:
theme_custom <- function(){
#font <- "Georgia" # assign font family up front
theme_minimal() %+replace% #replace elements we want to change
# custom elements go here
theme(
)
}
Now with the question's settings.
theme_user3353556 <- function(){
font <- "Arial" # assign font family up front
theme_minimal() %+replace% #replace elements we want to change
# custom elements go here
theme(
panel.grid.major = element_blank(),
text = element_text(size = 8, color = "black"),
axis.title.y = element_text(margin = margin(0, 20, 0, 0)),
panel.grid.minor = element_blank(),
#axis.ticks = element_blank(),
legend.key.size = unit(0.1, "in"),
legend.spacing.x = unit(0.01,"in"),
legend.position = "bottom",
plot.title = element_text(hjust = 0.5)
)
}
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
theme_user3353556()
I have two plots I would like to combine. My data looks like this:
Year<-rep(2001:2005, each = 5)
name<-c("John","Ellen","Mark","Randy","Luisa")
Name<-c(rep(name,5))
Value<-sample(seq(0,25,by=1),25)
mydata<-data.frame(Year,Name,Value)
This is the first barplot:
tot<-aggregate(mydata$Value,list(mydata$Year),FUN=sum)
tot_y<-tot$x
tot_x<-tot$Group.1
tot_barplot <- ggplot(tot, aes(x=tot_x,y=tot_y)) +
geom_bar(stat = "identity",fill="#73D055FF") +
scale_y_continuous(limits = c(0, 125), breaks = seq(0, 125, by = 25)) +
#xlab("Pathways") +
#ylab("N° of species") +
theme(axis.line = element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_text(size=14,margin=margin(l=10),colour="black"),
axis.ticks = element_blank(),
axis.title=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())
tot_barplot
And this is the second plot:
p <- ggplot(mydata, aes(x=Year, y=Name, size = Value)) +
geom_point(aes(fill = Value,
alpha = I(as.numeric(Value > 0))), shape=21, colour = "black") +
scale_fill_viridis_c(option = "D", direction = -1,limits = c(1, 25), breaks=seq(1, 25, 5))+
scale_size_area(guide = "none") +
ylab("Name") +
theme(axis.line = element_blank(),
axis.text.x=element_text(size=11,margin=margin(b=10),colour="black"),
axis.text.y=element_text(size=13,margin=margin(l=10),colour="black",
face="italic"),
axis.ticks = element_blank(),
axis.title=element_text(size=18,face="bold"),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.text = element_text(size=14),
legend.title = element_text(size=18))
p
I combine them like this:
grid.arrange(arrangeGrob(tot_barplot,p,nrow=2))
Now I would like to re-size the barplot to fit it better to the second plot (imagine that the original data produce a wider barplot where the bars start above the Name and end above the legend Value). I would like the bars of the barplot to be exactly centred above the line of points and the Year, but I am not very familiar with ggplot aesthetics.
Any suggestion would be appreciated. Thanks!
I am a bit stuck with the following: I want to arrange multiple (gg)plots into a grid with cowplot::plot_grid. Below is an example with two ggplots (g_bottom and g_top), both faceted. The bottom one has the facet labels deleted as they are redundant. However, there seems to remain the contour of the background or so, acting as cutting with a white line the top plot (see image below).
How could I fix this?
What have I tried so far:
Instead of strip.background = element_blank() I also tried strip.background = element_rect(fill = NA, color = NA) in theme, but with no success.
If I set rect = element_blank(), it somehow works, but I lose the entire plot border. I was then hopping that rect = element_rect(fill = "transparent", colour = NA) would do it, but still no success. I also just tried colour = NULL or colour = "transparent" also with no success.
library(ggplot2)
library(cowplot)
g <- ggplot(mpg, aes(class)) +
geom_bar() +
facet_grid(. ~ year) +
theme_bw()
g_bottom <- g +
theme(
strip.text = element_blank(),
strip.background = element_blank(),
# strip.background = element_rect(fill = NA, color = NA) # didn't work either
# Was hoping that this will do the trick, bot no success:
rect = element_rect(fill = "transparent", color = NA)
)
g_top <- g +
labs(x = element_blank()) +
theme(
axis.text.x = element_blank(),
axis.ticks.x = element_blank()
)
plot_grid(g_top, NULL, g_bottom,
# used NULL to be able to tweak spacing between plots with rel_heights
align = "hv",
nrow = 3,
rel_heights = c(1, -0.2, 1))
I could go around the problem by not faceting and creating each of the 4 plots individually, but maybe there is a more straightforward solution with some theme argument which I'm too blinded to see any-further...
Eventually, using rect = element_blank() in theme when making g_bottom and then adding panel.border = element_rect(colour = "black") seems to do the trick. I still fail to understand why the initial trials didn't work as expected.
library(ggplot2)
library(cowplot)
g <- ggplot(mpg, aes(class)) +
geom_bar() +
facet_grid(. ~ year) +
theme_bw()
g_bottom <- g +
theme(
strip.text = element_blank(),
rect = element_blank(),
panel.border = element_rect(colour = "black")
)
g_top <- g +
labs(x = element_blank()) +
theme(
axis.text.x = element_blank(),
axis.ticks.x = element_blank()
)
plot_grid(g_top, NULL, g_bottom + theme(panel.border = element_rect(colour = "black")),
align = "hv",
nrow = 3,
rel_heights = c(1, -0.2, 1))
I would like to export this graph from RStudio on a larger resolution but when I do this the tick marks of the x-axis separate from the axis.
I have looked at documentation for theme() but did not find a relevant function: http://docs.ggplot2.org/0.9.3/theme.html
So how can I keep my ticks connected to the x-axis?
I tried using the space between ticks and axis labels but it just changes the relevant spacing, but does not change the spacing I am talking about.
Code to generate the images I depicted below:
require(data.table)
require(ggplot2)
require(grid)
setsTimeline <- data.table(Set=c("Training Set","Validation Set","Test Set","Training Set","Validation Set","Test Set","Training Set","Validation Set","Test Set","Training Set","Validation Set","Test Set","Training Set"),
StartDate=c(1380708900,1402963200,1420070400,1421280000,1410912000,1396310400,1397520000,1418860800,1404172800,1405382400,1395100800,1412121600,1413331200),
EndDate= c(1395099900,1404171900,1421279100,1430985600,1412120700,1397519100,1402962300,1420069500,1405381500,1410911100,1396309500,1413330300,1418859900))
setsTimeline[,StartLabel:=as.POSIXct(StartDate,tz="UTC",origin="1970-01-01")]
setkey(setsTimeline,StartDate)
breaks <- c(1380708900,1395100800,1402963200,1410912000,1418860800,1430985600)
labels <- as.POSIXct(breaks,tz="UTC",origin="1970-01-01")
ggplot(setsTimeline, aes(colour=Set)) +
geom_segment(aes(x=StartDate, xend=EndDate, y="group", yend="group"), size=10) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
aspect.ratio=5e-02,
axis.text.x = element_text(colour='black', angle = 45, size = 16, hjust = 1, vjust = 1),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
legend.text = element_text(colour='black', size = 16),
legend.title = element_text(colour='black', size = 0),
legend.position = 'top',
plot.title = element_text(colour='black', size = 18),
plot.margin = unit(c(0,1,0,3.5), "cm")) +
xlab(NULL) +
ylab(NULL) +
ggtitle(NULL) +
coord_cartesian(xlim = c(1380708900,1430985600), ylim = NULL) +
scale_x_continuous(breaks=breaks,labels=labels)
Small res:
High res: