This question already has answers here:
move subplots closer together with R
(2 answers)
Closed 9 years ago.
I am using base R plot function to generate vertically aligned plots (2-by-1), with both x and y labels but without a title. However, when I use
par(mfrow=c(2,1))
plot(obj1)
plot(obj2)
I notice that the blank margin area between the two plots is so huge (I assume R does not exclude the area for the main= title...). Is there a way to make the two plots more "closer" to each other, while keep the x-labels at appropriate locations?
BTW, is there a way to generate a plot in PDF format without any useless blank margins? I plan to include the figure in the paper manuscript, and I don't want to see such margins occupying. Thanks!
As described in the (duplicate) question I linked to, the solution is exactly the same as there:
par(mfrow=c(2,1))
par(mar = c(0,4,4,2) + 0.1)
plot(1:5)
par(mar = c(5,4,0,2) + 0.1)
plot(1:5)
Related
This question already has answers here:
geom_bar bars not displaying when specifying ylim
(4 answers)
Closed 8 months ago.
I am trying to create a barplot using ggplot2, with the y axis starting at a value greater than zero.
Lets say I have the means and standard errors for hypothetical dataset about carrot length at three different farms:
carrots<-NULL
carrots$Mean<-c(270,250,240)
carrots$SE<-c(3,4,5)
carrots$Farm<-c("Plains","Hill","Valley")
carrots<-data.frame(carrots)
I create a basic plot:
p<-ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_bar(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0)
p
This is nice, but as the scale runs from 0 to it is difficult to see the differences in length. Therefore, I would like to rescale the y axis to something like c(200,300). However, when I try to do this with:
p+scale_y_continuous('Length (mm)', limit=c(200,300))
The bars disappear, although the error bars remain.
My question is: is it possible to plot a barplot with this adjusted axis using ggplot2?
Thank you for any help or suggestions you can offer.
Try this
p + coord_cartesian(ylim=c(200,300))
Setting the limits on the coordinate system performs a visual zoom;
the data is unchanged, and we just view a small portion of the original plot.
If someone is trying to accomplish the same zoom effect for a flipped bar chart, the accepted answer won't work (even though the answer is perfect for the example in the question).
The solution for the flipped bar chart is using the argument ylim of the coord_flip function. I decided to post this answer because my bars were also "disappearing" as in the original question while I was trying to re-scale with other methods, but in my case the chart was a flipped one. This may probably help other people with the same issue.
This is the adapted code, based on the example of the question:
ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_col(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0) +
coord_flip(ylim=c(200,300))
Flipped chart example
This question already has answers here:
Set R plots x axis to show at y=0
(2 answers)
Closed 3 years ago.
I observed that the x axes of a plot doesn't cross the y axes at 0.
Why?
How can I fix that?
Example:
plot(mtcars$mpg, ylim=c(0,50))
By default, R extends the axes by 4% on either end around the limits: from ?par,
‘xaxs’ The style of axis interval calculation to be used for the
x-axis. Possible values are ‘"r"’, ‘"i"’, ‘"e"’, ‘"s"’,
‘"d"’. The styles are generally controlled by the range of
data or ‘xlim’, if given.
Style ‘"r"’ (regular) first extends the data range by 4
percent at each end and then finds an axis with pretty labels
that fits within the extended range.
Style ‘"i"’ (internal) just finds an axis with pretty labels
that fits within the original data range.
(yaxs does the same thing for the y-axis).
You can use
plot(mtcars$mpg, ylim=c(0,50), yaxs="i")
This question already has answers here:
ploting artefact with points over raster
(2 answers)
Closed 8 years ago.
If I use plot() to draw a GIS raster image, then use points() to add some points on the image: E.g. the following code
in_rast_str <- "PET_eclp.tif"
in_rast <- raster(in_rast_str)
selected_cells <- choose_points(in_rast_str,10,30)
plot(in_rast)
points(selected_cells[,1],selected_cells[,2])
The initial output renders correctly. However, if I then resize the plot either in the export window, or in the zoom window of RStudio, the points and the underlying raster shift relative to each other and become misaligned.
Is this a problem with RStudio or with R?
I'm guessing that this could be a bug rather than that I am doing something obviously wrong.
Update...
Here are some examples:
and the same plot, but resized
That issue with raster has been annoying me for years.
This doesn't exactly answer your question, but rasterVis provides a very pleasing (to me, anyway) workaround.
library(rasterVis)
r <- raster(matrix(runif(100), 10))
xy <- xyFromCell(r, which(values(r) > 0.9))
levelplot(r, margin=FALSE) + layer(sp.points(xy, pch=20, cex=2, col=1))
Resize and zoom around as much as you like - the points will stick to the correct cells.
This question already has an answer here:
Needle plot in ggplot2 [duplicate]
(1 answer)
Closed 8 years ago.
I am trying to make a plot x,y stick plot (like the link in the bottom of the post). I am attempting to make a Mass spectrum plot (basically a stick plot), is there an easy way of doing this with ggplot2? I know how to make the plot with the plot() function in R, however, I would like to be able to make it with ggplot 2, as I need to do some additional modifications afterwards for which I require ggplot2.
So essentially I want to make a x,y stick plot. I am new to R, so bear with me.
Example of what I want
Try to upload some code and let us know where you are getting stuck. Also, please refer to the documentation or these examples.
That having been said, I believe this is close to what you want:
library(ggplot2)
ggplot(data = mtcars, aes(x = mpg)) +
geom_bar(binwidth = 1, color = 'white')
This question already has answers here:
Combined plot of ggplot2 (Not in a single Plot), using par() or layout() function? [duplicate]
(3 answers)
R: How should I create Grid-graphics?
(2 answers)
Closed 8 years ago.
I am trying to plot a scatterplot and box plot of two continuous varaibles but am getting an error that says,
Warning message:
In par(fig = c(0, 0.8, 0.55, 1), new = TRUE) :
calling par(new=TRUE) with no plot
The code worked when I simply replaced lines 4-6 of my code with:
plot(mydata$gre, mydata$gpa, xlab="GRE",ylab="GPA")
Here's my code:
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
par(fig=c(0,0.8,0,0.8), new=TRUE)
#plot(mydata$gre, mydata$gpa, xlab="GRE",ylab="GPA")
d<-ggplot(mydata,aes(x=mydata$gre, y=mydata$gpa))
d<-d+geom_line()
d
par(fig=c(0,0.8,0.55,1), new=TRUE)
boxplot(mydata$gre, horizontal=TRUE, axes=FALSE)
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mydata$gpa, axes=FALSE)
mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)
Could you please shed some light on what I am doing wrong with regards to ggplot since R isn't recognizing it? What's really weird is that when I type d, the name of my ggplot, I get a plot...
You are attempting to combine grid plots (ggplot2) and base plots (boxplot), but those 2 types of plotting do not play nicely together (hence the cryptic warning).
The simplest solution is to only use one of grid or base plots by replacing the call to ggplot with a call to plot or other functions (base only option) or using grid based functions to do the boxplots (lattice package also uses grid) then use functions from the grid package to arrange the multiple plots.
If you really want to combine grid and base plots then you can use the gridBase package, but this is going to require understanding both types of graphics quite well.