I would like to be able to draw borders on geom_tile that do not overlap so that borders can convey their own information without confusing the viewer with disappearing borders.
library(ggplot2)
state <- data.frame(p=runif(100), x=1:10, y=rep(1:10, each=10), z=rep(1:5, each=20))
ggplot(state, aes(x, y)) +
geom_tile(aes(fill = p, color=as.factor(z)), size=2)
I trust you can see how confusing overlapping borders can be.
Use the width and height arguments to geom_tile to create space and prevent border overlap.
ggplot(state, aes(x, y)) +
geom_tile(aes(fill = p, color=as.factor(z), width=0.7, height=0.7), size=2)
I've created space between the tiles, but you can also adjust the width and height so that the borders just touch each other. Also, note that in my version of the graph the color legend for tiles does not have any colors. This is because the border color legend for geom_tile is broken in ggplot2 2.0. The problem has been fixed in the development version of ggplot2, but the fix hasn't been rolled out to CRAN yet. To get the development version, you can do devtools::install_github("hadley/ggplot2").
(Incidentally, if you're into optical illusions, the graph below creates the grid illusion).
Related
I have created a basic scatterplot with ggplot2 and added a rug with geom_rug on the y-axis (left side) to it, however the rug is hiding a few points. I have tried position_dodge, position_jitterdodge and hjust in order to move the rug and make those points visible, but it did not give the desired result. I can't move the rug to the right side of the plot, since it will be hiding points here as well.
Is there a way to move the rug on the other side of the y-axis, outside of the plot?
Here is some example code:
data("midwest", package = "ggplot2")
ggplot(midwest, aes(x=area, y=poptotal)) + geom_point(alpha=0.8)+
geom_rug(aes(x=NULL, y=poptotal), alpha=0.8)
I solved the problem by simply expanding the x-axis limits, using
scale_x_discrete(expand = c(0.01,0.01))
I'm having a dataframe samp, with userid, latitude, longitude, mb. I wanted to plot a map with the points proportional to MB used. I wanted a donut kind of shape in geom_point, so I thought I can use two pch = 20 with varying sizes to get the donut shape of pch. But I am facing some problems in it.
m <- get_map(location=c(lon=median(samp$longitude),lat=median(samp$latitude) ), zoom=10)
print(ggmap(m) +
geom_point(aes(x=longitude, y=latitude, size= mb.user), colour="orange", pch = 20, data=samp) +
geom_point(aes(x=longitude, y=latitude, size= mb.user), colour="black", pch = 20, size = 4, data=samp))
but I am getting something like,
The shapes are not even throughout the map. I want the shapes to be even and proportional to mb.user values. But the map here is neither proportional to mb.user or the sizes.
Also the legend is also showing only one color. It isn't showing two colours together. I ideally want to have a donut shaped symbol whose size is proportional to mb.user.
Can anybody help me in finding the mistake I am doing here?
Thanks
If you use a point shape that has a border, you can plot the points just once and it will show up properly in the legend. If you have ggplot2 version 2 installed (latest version is 2.1.0 as of this writing), you can also control the width of the point border using the stroke parameter. You haven't provided a reproducible example, so here's an example using the built-in mtcars data frame:
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(size=mpg), colour="red", fill="black", shape=21, stroke=1.5) +
scale_size_area(max_size=4)
shape=21 is a filled circle with a border (see ?pch for available shapes). colour sets the border color, fill sets the fill color, and stroke sets the border width.
Regarding your original code, the black circles are all the same size, because you've overridden size=mb.user by also setting size=4 outside the call to aes. You can't see some of the orange points in cases where the black points are larger than the orange points. If you remove size=4 and do size=0.3*mb.user inside aes, you'll get properly scaled black points inside the scaled orange points.
However, that still won't solve the legend issue. I don't think there's a way to get a legend with black-inside-orange points using two separate calls to geom_point since there's no way (at least none that I can think of) to create a combined size/color mapping to do that. Using a single call geom_point with a filled marker solves the problem, but I thought I'd try to explain as best I could why your original code wasn't working as you expected.
I am polishing my graphs and have a problem with fitting direct labels in the plotting area. A want to remove most of the area between y1 and the y-axis to the left in the plot similar to that generated by the code below, but keep the extra area to the right to have room for the labels.
Adding +scale_x_discrete(expand=c(0,0.05)) removes extra area on both sides, but leaves no room for labels, and it can't seem to remove it on only one side.
Adding margins to the right of the plotting area with +theme(plot.margin = unit(c(0,4,0,0), "cm")) still doesn't allow the labels to appear there.
A solution that places the labels outside, to the right of the border would be even better.
Any help much appreciated.
library(ggplot2)
library(directlabels)
library(reshape2)
theme_set(theme_bw())
# some data
dfr<-data.frame(c("Longish Name A","Longish Name B","Longish Name C"),c(1,1,1),c(1,2,3),c(2,3,4))
colnames(dfr) <- c("subject","y1","y2","y3")
dfr<-melt(dfr, id.vars="subject")
# the graph
ggplot(data=dfr,aes(y=value, x=variable, group=subject)) +
geom_line(aes(color=subject))+
geom_dl(aes(label=subject), list(dl.trans(x=x+0.2), "last.qp", cex=0.5)) +
guides(color=FALSE)
Convert your x values to numeric inside the aes() and then use scale_x_continuous() to get back to original labels and set limits= that are wider on side.
ggplot(data=dfr,aes(y=value, x=as.numeric(variable), group=subject)) +
geom_line(aes(color=subject))+
geom_dl(aes(label=subject), list(dl.trans(x=x+0.2), "last.qp", cex=0.5)) +
guides(color=FALSE)+
scale_x_continuous(breaks=c(1,2,3),labels=c("y1","y2","y3"),expand=c(0,0.05),
limits=c(1,3.4))
I'm trying to plot a pie chart using GGPLOT2 in R. I want to do this in such a way as to omit the extra margin space.
What I'm doing is similar to what sharoz did in this post here except I want to include a legend.
Here is what I'm doing:
ggplot(DATA, aes(x=factor(0),fill=factor(LABELS),weight=VALUES)) +
geom_bar(width=1) +
coord_polar(theta='y') +
guides(fill=guide_legend(title='LEGEND'))
Assuming you are talking about the extra white space above and below the figure, the easiest solution is just to tweak the size of the graphics device. Here is aspect ratio is the key. If the aspect ratio of the graphics device matches that of the plot, you get rid of a lot of the whitespace.
What I use to save the plot is ggsave, in code:
ggplot(DATA, aes(x=factor(0),fill=factor(LABELS),weight=VALUES)) +
geom_bar(width=1) +
coord_polar(theta='y') +
guides(fill=guide_legend(title='LEGEND'))
ggsave("plot.png", width = 10, height = 5)
Just play around with width and height in ggsave until you are happy with the result.
Is it possible to shrink y-axis? I mean instead of the plot being square, I want it to be rectangular, with y axis shrinked.
library(ggplot2)
data = data.frame(rnorm(10000))
colnames(data) = "numOfX"
m <- ggplot(data, aes(x=numOfX))
m + geom_histogram(colour = "blue", fill = "white", binwidth = 0.5)
last_plot() + opts(aspect.ratio=1/10)
You can adjust the margins by adding the following to the last line of your code:
+ opts(plot.margin = unit(c(1,1,10,1), "lines"))
The numbers are the number of lines to add to the margin in the order c(top, right, bottom, left).
Update: The methods that baptiste and I discussed will change the size of the plot itself, but not the size of the plot area. Just for completeness, if you want to change the aspect ratio of the plot but still have it fill the whole plot area, then you need to change the size of the plot area itself.
On the Mac you can do quartz(width=w, height=h), with width and height in inches. This will open a plot window of the specified size. Then run your original code (without margin or aspect ratio changes). This will give you whatever plot size you wish and the plot will fill the plotting area. You can use dev.off() to close the Quartz window when you're done with it.
You can do the same thing in Windows using this Stack Overflow answer.
Finally, if you're using RStudio, you can do Export-->Copy-to-Clipboard and then adjust the aspect ratio manually.
Of course, you can use a combination of my or baptiste's original answers along with the methods above to control both the size of the plot area and the size of the margins at the same time.