Related
I want to save a picture from a plot which only retains the plot panel contents.
But when I used the below code, there's a white margin at the left and bottom of the plot.
How do I get rid of it?
Thanks.
ggplot()+
stat_function(fun = ~ -(.x-2) ^ 2 + 3, size = .5, linetype = 'dashed', color='white') +
scale_x_continuous(expand = c(0, 0), limits = c(0, 4)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 4)) +
theme(
plot.margin = unit(c(0,0,0,0), "pt"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "black", colour = "black"),
axis.text = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank()
)
I have a graph with four lines with two different linetypes (solid and dashed) and two colors. I want to display a table below the graph containing some information about each curve, e.g. :
The labels of the y-axis of this table are "S.2", "D.2",...
Is it possible to use labels (for the y-axis) that are examples of what the curve looks like instead of text?
Such as in the legend of the graph?
That is instead of "S.2" having a short blue dashed line?
Here is the code to produce the plot
require(ggplot2)
require(grid)
require(gtable)
## graph
g1<-ggplot() + aes(x=seq(0,5,.1),y=cumsum(sample(0:10,51,replace = T)),colour="1",linetype="S") + geom_line()+
geom_line(aes(x=seq(0,5,.1),y=cumsum(sample(0:10,51,replace = T)),colour="1",linetype="D"))+
geom_line(aes(x=seq(0,5,.1),y=cumsum(sample(0:10,51,replace = T)),colour="2",linetype="S"))+
geom_line(aes(x=seq(0,5,.1),y=cumsum(sample(0:10,51,replace = T)),colour="2",linetype="D"))+
scale_colour_manual(breaks=c("1","2"),values=c("#cd7118","#1874cd"))+
scale_x_continuous(limits = c(0,5),breaks = 0:5,expand=c(0,0))+
scale_linetype_manual(breaks=c("S","D"),values=c("solid","dashed"))+
theme(
axis.line = element_line(size = 0.2, linetype = 'solid',colour = "black"),
axis.text = element_text(size=10),
axis.title = element_blank(),
legend.title = element_blank(),
legend.background = element_blank(),
legend.key = element_blank(),
legend.position = c(.1,.5)
)
## table
count<-data.frame(time=rep(0:5,4),count=rep(0:5,4),col=c(rep("1",12),rep("2",12)),lintype=rep(c(rep("S",6),rep("D",6)),2))
g2<-ggplot(count, aes(x = time , y = interaction(factor(lintype),factor(col)), colour=factor(col), label=count)) +
geom_text(size=4) +
scale_colour_manual(breaks=c("1","2"),values=c("#cd7118","#1874cd"))+
scale_x_continuous(limits = c(0,5),breaks = 0:5,expand=c(0,0))+
theme(
legend.position = "none",
panel.background = element_rect(fill = "white",colour = "white",size = 0.5, linetype = "solid"),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(size=10, color = 'black',margin = margin(t = 0, r = 20, b = 0, l = 0)),
axis.ticks=element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.title = element_text(size=10),
plot.margin = margin(t=1,b=1,r=15,l=10,unit="pt")
)
#
merge = rbind(ggplotGrob(g1), ggplotGrob(g2), size="last")
panels <- merge$layout$t[grep("panel", merge$layout$name)]
merge$heights[panels[1]] <- unit(10,"cm")
merge$heights[panels[2]]<-unit(6,"lines")
merge <- gtable_add_rows(merge, heights = unit(1,"line"), 1)
merge$layout$clip[merge$layout$name=="panel"]<-"off"
grid.draw(merge)
I am trying to create a graph of value ranges so that they can be compared to a unique range (represented by a gray rectangle in the graph). I got the graph to work just like I wanted to it but I cannot effectively change the line weight of the error bars that I am using to represent the value ranges. The code it below with example figures.
### Graph with default line weights
ggplot(data=droplevels(wght2[which(wght2$Zone!="Oahu"),]), aes(x=wght2$num[wght2$Zone!="Oahu"], y=(wght2[which(wght2$Zone!="Oahu"),4]+wght2[which(wght2$Zone!="Oahu"),4+3])/2,
group= wght2[which(wght2$Zone!="Oahu"),2], color= wght2[which(wght2$Zone!="Oahu"),2])) +
geom_errorbar(aes(ymin=wght2[which(wght2$Zone!="Oahu"),4], ymax=wght2[which(wght2$Zone!="Oahu"),4+3], width=0.3, color=wght2[which(wght2$Zone!="Oahu"),2])) +
geom_rect(ymin=wght2[8,4], ymax=wght2[8,4+3], xmin=0.5, xmax=6+0.5, fill="gray25", linetype=0, alpha=0.07)+
scale_color_manual(values=c("forestgreen","deepskyblue3","gold"))+
scale_x_continuous(labels=levels(wght2$Zone), breaks=c(1,2,3.5,5.5))+
labs(x="Zone",y=measure3[4]) +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.text = element_text(size = 16),
legend.title = element_text(size=16),
axis.text = element_text(size = 16,colour="black"),
axis.title = element_text(size = 18)) +
theme(legend.position="topright")
### My attempt to change line weights
ggplot(data=droplevels(wght2[which(wght2$Zone!="Oahu"),]), aes(x=wght2$num[wght2$Zone!="Oahu"], y=(wght2[which(wght2$Zone!="Oahu"),4]+wght2[which(wght2$Zone!="Oahu"),4+3])/2,
group= wght2[which(wght2$Zone!="Oahu"),2], color= wght2[which(wght2$Zone!="Oahu"),2])) +
geom_errorbar(aes(ymin=wght2[which(wght2$Zone!="Oahu"),4], ymax=wght2[which(wght2$Zone!="Oahu"),4+3], width=0.3, size=0.5, color=wght2[which(wght2$Zone!="Oahu"),2])) +
geom_rect(ymin=wght2[8,4], ymax=wght2[8,4+3], xmin=0.5, xmax=6+0.5, fill="gray25", linetype=0, alpha=0.07)+
scale_color_manual(values=c("forestgreen","deepskyblue3","gold"))+
scale_x_continuous(labels=levels(wght2$Zone), breaks=c(1,2,3.5,5.5))+
labs(x="Zone",y=measure3[4]) +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.text = element_text(size = 16),
legend.title = element_text(size=16),
axis.text = element_text(size = 16,colour="black"),
axis.title = element_text(size = 18)) +
theme(legend.position="topright")
As you can see from my code when I include the 'size' argument in the 'aes' function of geom_errorbar, the line weights become huge even though I set it at the default which is 0.5.
Any advice on how to change the line weights in geom_errorbar would be much appreciated. I know I could just draw each line in with geom_segment but feel like using geom_errorbar should work and want to know why it is not. Thanks!
I am struggling with ggplot to produce the legend of this figure. For now, I am only doing for one map, when it works I'l produce the four maps in the same plot.
I would like a legend like this: bottom, title in the center and above the scale, labels and colors, and omit the NA values.
Here is my code:
Reading the shapefile and installing a new variable
map_ev#data$id = rownames(map_ev#data)
map_ev.points = fortify(map_ev, region="id")
map_ev.df = join(map_ev.points, map_ev#data, by="id")
map_ev.df$median_norm = map_ev.df$median / map_ev.df$VOM
Vector with theme opts for ggplot
theme_opts <- list(theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.title = element_text(size=12, hjust=0.5),
legend.position = "bottom",
legend.title=element_blank()))
Installing the variable breaks
map_ev.df$median_norm <- cut(map_ev.df$median_norm, breaks=c(-200, -100, -50, -20, -5, 0, 5, 20, 50, 100, +200))
Checking the breaks
levels(map_ev.df$median_norm)
Colors to be used
color_map <- palette(c("#5b2e07", "#904d07", "#b98436", "#dfc27e", "#f6e8c3",
"#c9e9e4", "#84cdc4", "#3c958f", "#01675a", "#073a31"))
ggplot code
ggplot(map_ev.df) +
aes(long,lat,group=group,fill=median_norm, color=median_norm) +
geom_polygon() + geom_path(color="black") +
labs(title="Equivalent variation") + coord_equal() +
theme_opts
For now I am getting this figure:
Thanks all, I appreciate your help!
I just noticed you wanted manual colours for the fill. You can use:
myscale <- c("(-1,10]"="#BBDF27", "(10,20]"="#43BF71", "(20,30]"="#21908C", "(30,50]"="#35608D", "(50,101]"="#482576", "(101,300]"="#ffeeed")
combined with:
scale_fill_manual(values=myscale,na.value="#e0e0e0",name="",labels=c("<10","10 - 20","20 - 30","30 - 50", ">50","Estimate", "NA"))
where scale_fill manual replaces scale_fill_brewer in the other answer.
Just replace the colour and label values with your own. The values that need to go into myscale are shown when you plot your map without any labels. Good luck!
The reason you don't have a title, is because you never specified one, and if you did, you're deleting it again by writing legend.title=element_blank() in theme. Instead I rewrote it to specify the title should be centered. Using guides(colour = guide_legend(title.position = "top"),fill = guide_legend(title.position = "top",nrow=1,byrow=TRUE)) I set the position to "top" and made sure it will only be one row of legend items. (I left colour= here in case you do need it).
You also seem to want black borders, this means I deleted all colour= since that specifies the colour of the borders.
I've added a scale_fill_brewer, there are more color palettes available. Within this you can specify your legend title and the appearance of your legend labels. These need to be manually adjusted every time you change your input data, by the way.
I don't know how to remove the legend item for NA, sorry.
This is the total code I came up with:
ggplot(map_ev.df) +
aes(long,lat,group=group,fill=median_norm) +
scale_fill_brewer(palette="BrBG",name= "Title of legend",labels=c("-200 - -100","-100 - 50","-5 - 0","0 - 5", "label for NA"))+
geom_polygon() + geom_path(color="black") +
labs(title="Equivalent variation") + coord_equal() +
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.title = element_text(size=12, hjust=0.5),
legend.position = "bottom",
legend.direction = "horizontal",
legend.title = element_text(hjust=0.5))+
guides(colour = guide_legend(title.position = "top"),fill = guide_legend(title.position = "top",nrow=1,byrow=TRUE))
Using some data of my own for the fill:
map
I am trying to remove the y-axis on two ggplots that are in a gtable with a third ggplot. I would like to show the y-axis for the leftmost graph in the gtable and remove the y-axis completely from the subsequent graphs; however, I would like the x-axis to remain on all plots.
My graph looks like this:
![nucleotide diversity][1]
[1]: image produced by code
library("ggplot2")
library("gridExtra")
library("gtable")
theme_set(theme_bw(base_size=16))
p1 <- ggplot(a.pi, aes(x=window, y=measure, fill=key, colour=key)) +
geom_line() +
scale_colour_manual(values=c("#000099", "#333333", "#FF0000")) +
ylab(expression(pi)) +
xlab("Position") +
scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
scale_y_continuous(limits=c(0.0,0.0004)) +
theme(#axis.text.y = element_blank(),
#axis.ticks.y = element_blank(),
#axis.title.y = element_blank(),
#axis.title.x = element_blank(),
plot.margin = unit(c(0,-3,0,0), "lines"),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position="none",
axis.line = element_line()
)
p2 <- ggplot(b.pi, aes(x=window, y=measure, fill=key, colour=key)) +
geom_line() +
scale_colour_manual(values=c("#333333", "#FF0000")) +
#ylab(expression(pi)) +
xlab("Position") +
scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
scale_y_continuous(limits=c(0.0,0.0004)) +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
#axis.title.x = element_blank(),
plot.margin = unit(c(0,-3,0,0), "lines"),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position="none",
axis.line = element_line()
)
p3 <- ggplot(c.pi, aes(x=window, y=measure, fill=key, colour=key)) +
geom_line() +
scale_colour_manual(values=c("#333333", "#FF0000")) +
#ylab(expression(pi)) +
xlab("Position") +
scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
scale_y_continuous(limits=c(0.0,0.0004)) +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
#axis.title.x = element_blank(),
plot.margin = unit(c(0,-3,0,0), "lines"),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position="none",
axis.line = element_line()
)
grid.arrange(p1,p2,p3, nrow=1)
gt1 <- ggplot_gtable(ggplot_build(p1))
gt2 <- ggplot_gtable(ggplot_build(p2))
gt3 <- ggplot_gtable(ggplot_build(p3))
newWidth = unit.pmax(gt1$widths[1:3], gt2$widths[1:3], gt3$widths[1:3])
gt1$widths[1:3] = as.list(newWidth)
gt2$widths[1:3] = as.list(newWidth)
gt3$widths[1:3] = as.list(newWidth)
# New gtable with space for the three plots plus a right-hand margin
gt = gtable(widths = unit(c(1, 1, 1, 0.3), "null"), height = unit(1, "null"))
# Instert gt1, gt2 and gt2 into the new gtable
gt <- gtable_add_grob(gt, gt1, 1, 1)
gt <- gtable_add_grob(gt, gt2, 1, 2)
gt <- gtable_add_grob(gt, gt3, 1, 3)
grid.newpage()
grid.draw(gt)
Your linked image is not showing, but here is my shot in the dark:
Change from this:
plot1 <- theme(#axis.text.y = element_blank(),...
to this:
plot1 <- theme(axis.text.y = element_text(),...
if you want to change the label do this:
plot1 <- ... + ylab("y-axis label")
You need to do two things:
One is set axis.title.x and axis.title.y as blank under options (opts).
The other one is set xlab("") and ylab("")
I have included a code snippet in case it helps:
ggplot(space[1:closetos[i],], aes(dim1, dim9, colour = name,
shape=shape))+ opts(axis.line = theme_segment(colour = "black"),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
panel.border = theme_blank(),
panel.background = theme_blank(),
axis.title.x = theme_blank(),
axis.title.y = theme_blank())+
xlab("") +
ylab("")+
theme(text = element_text(size=15, colour = "black"),
axis.text.x = element_text(angle=0, vjust=1, colour = "black"),
axis.text.y = element_text(angle=0, vjust=1, colour = "black"),
axis.line = element_line(colour = 'black', size = 1),
axis.ticks = element_line(colour = 'black', size = 1),
axis.ticks.length = unit(0.3, "cm"),
axis.title.y=element_text(vjust=0.4),
legend.position = "none") +
geom_point(size=5)+
scale_color_manual("Status", values = mycolours) +
xlim((space$dim1[closetos[i]]-0.01), (space$dim1[closetos[i]]+0.01)) +
ylim((space$dim9[closetos[i]]-0.01), (space$dim9[closetos[i]]+0.01))