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')
Related
I have a question about facet_wrap() in ggplot2.
I am trying to make a graph that looks like this. I attach an example image 1.enter image description here
In image 1 it can be seen that there are two maps and each one has its legend and color scale. I would like to be able to do this with ggplot and the facet_wrap() function.
My problem is that because the data in the dataframe is very different, they have a lot of amplitude for each map, when plotting the scale it does not allow me to visualize it the way I want.
enter image description here
ggplot(dataframe,mapping=aes(x=lon,x=lat))+
geom_contour_fill((aes(z=hgt,fill=stat(level)))+
geom_contour(aes(z=hgt),color="black",size=0.2)+
scale_fill_distiller(palette = "YlOrBr",direction = 1,super=ScaleDiscretised)+
mi_mapa+
coord_quickmap(xlim = range(dataframe$lon),ylim=range(dataframe$lat),expand = FALSE)+
facet_wrap(~nombre_nivel,scales="free", ncol =2) +
labs(x="Longitud",y="Latitud",fill="altura",title = "campos")
my dataframe has a shape like this. Where the facets are determined by the level variable. In this case the dataframe has another variable which is temp instead of hgt, but it's just another name.
enter image description here
Thanks
I think I've faced the alike problem building the two parts of the single map with two different scales. I found the package grid useful.
library(grid)
grid.newpage()
print(myggplot, vp = specifiedviewport)
In my case I built the first p <- ggplot() than adjusted p2 <- p + ...
with printing the two ggplots (p and p2) in two viewports. You can newly construct p2 with individual scale and print it in the grid. You can find useful information
here.
This question already has answers here:
Change colors in ggpairs now that params is deprecated
(2 answers)
Closed 1 year ago.
I'm using the ggpairs() function from the GGally package, but I'm having an issue dealing with overplotting.
Is there a good way to address the overplotting? I've tried setting the alpha value but I couldn't find out how to pass it to ggpairs(). I also looked into using geom_hex but again, I couldn't see how to use it with ggpairs().
Here's a simple example:
# Create example data
df <- data.frame(x1=rnorm(1e4),
x2=rnorm(1e4),
x3=runif(1e4))
# Pairs plot
GGally::ggpairs(df)
To reduce overplotting of the points you may modify the size aesthetic in point based layers displayed in the lower triangular of the plot matrix:
GGally::ggpairs(df, lower=list(continuous=GGally::wrap("points", size = .01)))
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
I am drawing a PC plot using ggplots.
I know this question has been answered in some previous posts but I could not still solve my problem.
I have a data set called tab which is the output of PCA
sample.id pop EV1 EV2
HT185_MK8-2.sort.bam HA_27 -0.03796869 0.046369552
HT48_SD1A-37.sort.bam HA_14 0.04208393 0.032961404
HT53_IA1A-10.sort.bam HA_1 -0.02580365 0.005262476
HT260_MK1-4.sort.bam HA_20 -0.06090545 0.005578504
HT170_SD2W-14.sort.bam HA_17 0.01288395 0.012117833
Q093_MK7-13.sort.bam HA_26 0.06310162 0.188558067
I want to add labels on each dot in the plot, theses dots are individuals from several populations. So I want to give them their population ID (pop column in the data set).
I am using something this
ggplot(data=tab,aes(EV1,EV2, label=tab[,2])) + geom_point(aes(color=as.factor(pop))) + ylab("Principal component 2") + xlab("Principal component 1")
But I do not get my desired output.
This is my PC plot!
So could anyone help me to add population label on each dot in the plot!
Thanks
Try geom_text:
geom_text(aes(label=as.character(pop)),hjust=0,vjust=0)
Also consider looking into plotly, or setting a threshold on the labels, because labeling every point will lead to a very crowded plot, and probably very little additional useful information.
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)