I'm relatively new to ggplot2, and I'm having trouble adding appropriate labels to my contours. I would love to be able to add the labels without the directlabels package, but I haven't found a way to, so if you know of a way to customize labels without directlabels, I would love to here it.
Using the classic volcano example, I can add labels to the default contour plot using the directlabels packet in the following way:
library(plyr)
library(ggplot2)
library(directlabels)
library(reshape)
volcano<-melt(volcano)
v<-ggplot(volcano, aes(x,y,z=z))
e<-v + stat_contour(aes(colour=..level..))
direct.label(e)
In the above example, the labels are added appropriately, but things become more complicated if I try to specify my own break points for the contours:
e<-v + stat_contour(aes(breaks=c(160, 170, 180), colour=..level..))
direct.label(e)
Now, the contours are specified by the breaks I have provided, but labels still appear for all of the default contours. How do I only plot only labels for the graphed contours?
A related issue, how would I plot labels for contour levels not included in the default? Say a break of 165:
e<-v + stat_contour(aes(breaks=c(165), colour=..level..))
direct.label(e)
Thanks for any help!
The current development version (directlabels_2013.6.15 with ggplot2_0.9.3.1) should fix your problem (as the author of the directlabels package explained to me). You can install it with:
install.packages("directlabels", repos="http://r-forge.r-project.org")
And then:
library(plyr)
library(ggplot2)
library(directlabels)
library(reshape)
volcano<-melt(volcano)
v<-ggplot(volcano, aes(X1,X2,z=value))
e<-v + stat_contour(aes(colour=..level..), breaks=c(165))
direct.label(e)
I noted several other limitations with simple workarounds:
the first ggplot call must contain the z aesthetic
this works only with the stat_contour (and not with the geom_contour)
the colour aesthetic must be defined in the stat_contour call and set to ..level..
Finally, if you want to control the label and contour line colours (black labels and blue contour lines for instance), you can achieved this as follow:
e<-v + stat_contour(aes(colour=..level..), colour = "blue", breaks=c(165))
e<-e + scale_colour_continuous(low = "#FF0000", high = "#FF0000")
direct.label(e)
Related
I try to generate a plot on which every point stands for an event. Color, Size and faced_grid are used to give additional information available in a visual way. The graph is working in ggplot2 but it is often important to know the exact numbers so an interactive version is needed which enables to hover over the point and get the info. I tried to convert the plot into an interactive version with the function ggplotly from the plotly-package. The problem then is, that the legend not only display the different states of the used attributes, it contains every existent combination. In addition, it did not display info from geom_rect.
I found related/similar questions but they used the function plot_ly and not ggploty or did not provide an answer.
Following, the same problem illustrated with the mtcars dataset:
library(plotly)
g = ggplot(mtcars,aes(x=mpg,y=disp,color = as.factor(cyl),size =as.factor(gear))) +
geom_point() +
geom_text(label = c(rep("A",nrow(mtcars)-5),rep("B",5)),color = "black",size=4) +
geom_rect(data=data.frame(name="zone",Start=20,End = 30,ymin = -Inf,ymax = Inf),aes(xmin=Start, xmax=End, ymin=ymin, ymax=ymax,fill=name),inherit.aes = FALSE,alpha=0.3)+
facet_grid(vs~am)
g
This is the result and how it should look like: ggplot Graph
Now using ggplotly
ggplotly(g)
This is the result: ggploty Graph
(1) The legend is now a combination of the different attributes used for Color and Size
(2) geom_rect is in the legend but didn’t get displayed in the graph
Does anyone knows how to get the same graph in ggplotly like in ggplot2? I am grateful for every hint. Thanks
Dave
I do not know how to fix the combination of legends when you use ggplotly. But, I can fix the second problem, if you do not use the Inf and -Inf, the geom_rect will work:
ggplotly(ggplot(mtcars,aes(x=mpg,y=disp, = as.factor(cyl),size =as.factor(gear))) +
geom_rect(aes( xmin=20,
xmax=30,
ymin=0,
ymax=max(mtcars$disp),
fill="Name"),
inherit.aes = FALSE, alpha=0.3) +
geom_point() +
geom_text(label = c(rep("A",nrow(mtcars)-5),rep("B",5)), = "black",size=4) +
facet_grid(vs~am))
However, the legends are bad.
I would suggest using subplot to create the same thing in Plotly, and I think this link Ben mentioned will help you create each subplot. One thing to mention is that I had trouble Illustrating different size in legend in plotly, while the size of the marker will be different, there will not be a legend for the size scale. Maybe a scale will be a better option.
I want colourbars created with ggplot to be similar to what spplot function (from lattice package) creates. Something like the attached image with each finite number of colours being assigned to rectangular blocks, instead of creating a continuous spectrum of colours. I need to be able to define the outline colour of the colourbar and also the format of the ticks.
I put this simple example together. How can I change this into something similar to this attached image? For example, I want the legend to start from -3 and end at 3 with 10 blocks of colours. I already tried 'nbin' in the function 'guides'. But I need the labels to be put at the 'edges' of the colour blocks instead of at the middle of them (i.e. centre of the bins).
ps: And sometimes ggplot creates a labels beyond the length of the colourbar!
library(ggplot2)
dat <- data.frame(x = rnorm(100), y = rnorm(100), col=rnorm(100))
ggplot(dat, aes(x,y,color=col)) +
geom_point() +
scale_color_gradient2(limits=c(-3,3), midpoint=0) +
guides(color=guide_colourbar(nbin=10, raster=FALSE))
I think what you ask for is not possible using the latest (public) version of ggplot2.
Ugly method, do at your own discretion
However, if you install the development version (this led to some version conflicts with other packages on my machine and I guess some things are not fully working yet) using
devtools::install_github("tidyverse/ggplot2")
library(ggplot2)
You will get some more options to modify guides such as ticks.colour, frame.colour or frame.linewidth which lets you customize the colorbar according to your requirements:
set.seed(6)
dat <- data.frame(x = rnorm(100), y = rnorm(100), z=rnorm(100))
ggplot(dat, aes(x,y,color=z)) + geom_point() +
scale_color_gradientn(colours=c("blue","gray80","red"), limits=c(-3,3),
breaks=c(-3/9*8,-3/9*4,0,3/9*4,3/9*8), labels=c(-2.4,-1.2,0,1.2,2.4), na.value = "green",
guide=guide_colorbar(nbin=10, raster=F, barwidth=20, frame.colour=c("black"),
frame.linewidth=1, ticks.colour="black", direction="horizontal")) +
theme(legend.position = "bottom")
Use colours = c() to specify a vector of colors
Use breaks together with labels to manually assign labels at the correct positions along the colorbar. EDIT: We can easily compute the required position along the colorbar by dividing 3 (the length of one half along the colorbar) by 9 (there are 9 half-boxes from the middle of the bar to the centre of the first box) and multiplying that by the number of half-boxes where we want the label to appear.
Values outside of limits will be colored according to na.value
You could additionally specify name = "Your Variable Name" to replace the z next to the colorbar
I see no way to put -3 / 3 at the very ends of the color bar, other than manually placing a text element at the correct position in the plot (which I would strongly advice against).
plot(iris$Sepal.Length, iris$Sepal.Width, col = iris$Species)
I know that I can use the legend() function to manually set my legend. However, I have no idea which color was assigned to the different species in my data? Is there an automatic way to get plot() to add a legend?
As #rawr says, palette() determines the colour sequence used. If you use integers to specify colours, it will also look at palette(). Thus
with(iris,plot(Sepal.Length, Sepal.Width, col = Species))
legend("topright",legend=levels(iris$Species),col=1:3, pch=1)
works nicely.
Base R doesn't have an auto-legend facility: the ggplot2 package does.
library(ggplot2)
ggplot(iris,aes(Sepal.Length,Sepal.Width,colour=Species))+geom_point()
gives you a plot with an automatic legend (use theme_set(theme_bw()) if you don't like the grey background).
The built-in lattice package can also do automatic legends:
library(lattice)
xyplot(Sepal.Width~Sepal.Length,group=Species,data=iris,auto.key=TRUE)
I'm fairly new to ggplot2, and I'm trying to create a contour plot of data that has missing values. Because there's missing values I can't have the contours by themselves, so I'm combining a tiles background with a contour. The problem is the labels are the same colour as the background.
Suppose I have data like so:
DF1 <- data.frame(x=rep(1:3,3),y=rep(1:3,each=3),z=c(1,2,3,2,3,4,3,NA,NA))
I can make a plot like this:
require(ggplot2); require(directlabels)
plotDF <- ggplot(DF1,aes(x,y,z=z)) + geom_tile(aes(fill=z)) + stat_contour(aes(x,y,z=z,colour= ..level..),colour="white")
direct.label(plotDF)
This gives me a plot similar to what I want but I'd like to be able to change the colours of the labels to be black. Any ideas?
I spotted a similar post and thought this would be easy, something along the lines of direct.label(p, list("last.points", colour = "black"). I could not make it work, unfortunately; I believe, this is not directly supproted.
I then decided to use black magic and managed to do the trick by manually overriding the colour scale:
direct.label(plotDF +
scale_colour_gradient(low="black", high="black"))
As you can see from the screenshot, the X-Axis points are all jumbled. Is there a way to make them appear vertically so that they can be read?
And is there a way to specify the interval in the Y-Axis?
I am using ggplot2's qplot.
qplot(merged[[1]], merged[[4]], colour = colour, xlab="Positions", ylab ="Salary", main="H1B Salary 2012", ylim=c(50000,300000))
Without sample data and your actual code it is hard to give exact answer.
Generally, axis texts formatting is changed using function theme() and argument axis.text.x= (for x axis). To set breaks for y axis, scale_y_continuous() should be used with arguments breaks=.
qplot(mpg, wt, data=mtcars)+
theme(axis.text.x=element_text(angle=90,vjust=0.5))+
scale_y_continuous(breaks=seq(0,5,0.5))
More information about theme() features is on ggplot2 site.