I am trying to draw the two horizontal and vertical lines at 0.00 point using ggplot2,
I have tried this
theme <-
theme(
panel.grid = element_blank(),
axis.line.x = element_line(),
axis.line.y = element_line(),
panel.grid.major.x = element_line(),
panel.grid.major.y = element_line()
)
ggplot(data=eigenvec) + geompoint() + theme
I want only the two lines at 0.0, not the whole grid, how can I have that?
eigenvector data
PC1
PC2
0.651002
-0.613762
-0.52076
-0.21174
-0.482915
-0.202607
0.108556
0.439427
0.244979
0.586785
You manually added a grid with panel.grid.major which you should remove, and then you have to draw the lines at the coordinates you want with geom_hline and geom_vline :
theme <-
theme(
panel.grid = element_blank(),
axis.line.x = element_line(),
axis.line.y = element_line())
ggplot(data=eigenvec,aes(x=PC1,y=PC2)) + geom_point() + theme +
geom_hline(yintercept=0)+ geom_vline(xintercept=0)
(Pay attention that the code you shared was plenty of errors on ggplot formatting)
Related
I tried to plot a matrix using geom_tile. However, I noticed there are two strange blocks appear at the top and bottom of my plot. My initial guess was these are ticks element. I've tried to specify the theme parameters as far as I know but no luck.
Basically, I want to remove the two wired blocks that I marked in red arrow. The left plot is something that I desired except the white block. The right plot is I tuned the plot.background in theme to show you there are something I don't know occupies the area.
Image here
I also attached the minimal code that could reproduce the left plot:
test2 <- matrix(runif(100*100),nrow = 100)
testdf <- test2 %>% reshape2::melt()
testdf$Var2 <- factor(testdf$Var2,levels=(seq(max(testdf$Var2),1))) # you could ignore this line
testdf %>% ggplot() + geom_tile(aes(x=Var2,y=Var1,fill=log2(value+1))) +
scale_fill_gradientn(colors = c("#ffffff","#f9c979","#ec8121","#b80217","#2f0006")) +
theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.title = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=1),
plot.background = element_blank()) + coord_equal()
These blocks are the result of ggplot2's default expansion of the scale. To get rid of these block set the expansion to zero via scale_y_continuous:
library(ggplot2)
library(reshape2)
test2 <- matrix(runif(100*100),nrow = 100)
testdf <- reshape2::melt(test2)
testdf$Var2 <- factor(testdf$Var2,levels=(seq(max(testdf$Var2),1))) # you could ignore this line
ggplot(testdf) +
geom_tile(aes(x=Var2,y=Var1,fill=log2(value+1))) +
scale_y_continuous(expand = c(0,0)) +
scale_fill_gradientn(colors = c("#ffffff","#f9c979","#ec8121","#b80217","#2f0006")) +
theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.title = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=1),
plot.background = element_blank()) + coord_equal()
For this problem I am required to use ggplot2 and ggplot2 only. I have a barplot similar to the one below:
I would like to insert a title on the upper left corner but somehow it keeps ending up in the wrong place.
geom_text(stat='count', aes(label = ..count..), vjust = -1) +
theme(panel.background = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_text(),
axis.ticks.y = element_blank(),
axis.title.x = element_blank(),
plot.margin = margin(1,1,1,1),
panel.border = element_rect(fill = NA))
Furthermore I run into the problem of reshaping the plot. I would like the height of the plot to be much smaller but have the same width. Whenever I try it myself i can crop the plot but parts of the biggest bar are being cropped of aswell.
Can anyone help me?
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 create a full-page figure to publish. The figure looks great on my screen device using Cairo. I've struggled and struggled to get it saved nicely as a file that I can submit. I'm close, as well as I can articulate it, my points and lines just blob together. It's not a resolution issue because as I zoom in, everything looks better and better.
Here is how I make one panel (drawing from two different data frames):
ggplot()+
geom_line(aes(df1$Date, y=df1$sw_10), colour = "black", size=.25) +
geom_point(aes(df2$Date, y=df2$sw_10),fill="white",colour="blue",pch=21, size=1) +
scale_fill_manual(values=c("white"))+
ylim(0.1,0.4)+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(),
legend.position='none', legend.title=element_blank(),
legend.text = element_text(size=12),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(colour="black", size=12))
Then I put them together with grid and output as an eps:
postscript("figure.eps", width=8.5, height = 11)
print(fig)
dev.off()
And here is the result:
figure
or, as the .eps: https://www.dropbox.com/s/zo3rsnv7tuvg1a6/tryit.eps?dl=0
Is there any obvious way to improve this?
I'm trying to plot some overlapping density plots in ggplot2. I'm running into a problem where I cannot remove the diagonal slash from the legend. I have tried using scale_fill_manual() and legend.key as well as the hack from R Cookbook, but I cant seem to get it right.
data(iris)
iris=iris
cols=brewer.pal(3,"Set1")
ggplot(iris) +
geom_density(position="identity",aes(x=iris$Sepal.Length,fill=cols[1]),
colour="black",alpha=.5) +
geom_density(position="identity",aes(x=iris$Sepal.Width,fill=cols[2]),
colour="black",alpha=.5)+
theme_bw() +
scale_fill_identity(guide="legend",labels=c("Sepal Width","Sepal Length"))+
xlab("X axis") +
theme(panel.background=element_blank(),
legend.title=element_blank(),
legend.key = element_rect(),
legend.background = element_blank(),
legend.justification=c(1,0),
legend.position=c(.75,.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
What can I do to solve this?
Try this:
+ guides(fill = guide_legend(override.aes = list(colour = NULL)))
although that removes the black outline as well...which can be added back in by change the theme to:
legend.key = element_rect(colour = "black")
I completely forgot to add this important note: do not specify aesthetics via x=iris$Sepal.Length using the $ operator! That is not the intended way to use aes() and it will lead to errors and unexpected problems down the road.