I try to plot a graph in R using the ggplot2 package. Let's assume I have the following data:
df1<-data.frame(xpos=c(1,2,3,4),ypos=c(0.222,0.222,0.303,0.285))
Now, I want to plot a simple line graph:
ggplot(data=df1, aes(x=xpos, y=ypos)) +
geom_line()+
geom_point()
Now, when I adjust the y-axis:
+scale_y_continuous("Y rates upd", breaks=seq(0,1,0.2),limits=c(0,1))
The lines get "broken"
That's dependent upon your graphics device that plots are sent to from R. See the same plot below when I instead export it to PDF.
Related
I am trying to compare some data on a Q-Q plot with the regular distribution of the data and then a distribution with a log transformation of the same variable.
However, I am getting the same plot (though the y-axis has a different range, of course). The x-axis is the same in both plots.
Here is the code for the regular plot:
sbpqq <- wcgs %>%
ggplot() + geom_qq(mapping = aes(sample = sbp))
sbpqq
And here is the code for the log plot:
sbpqq <- wcgs %>%
ggplot() + geom_qq(mapping = aes(sample = log(sbp)))
sbpqq
The two plots, like I said, look the same, although I imagine they should look different (the log transformation on a histogram made the data follow a more normal distribution). Should the plots look the same and I'm just misinterpreting this?
Any help is appreciated.
Thank you!
Here are the plots:
Normal Plot:
Log Plot:
After viewing the two plots side-by-side in RStudio, I realized that they are not identical and that the log plot is more normal than the regular plot.
I've looked through the description and the book on ggplot2 and cannot find a simple way of eliminating the legend in a simple density plot with a filled color.
Here is what I've tried with a simple sequence of 1000 numbers (plotseries) that had about 200
NA in the first 200 spots.
qplot(plotseries,geom="density",fill="red",na.rm=TRUE,show_guide=FALSE)
qplot(plotseries,geom="density",fill="red",na.rm=TRUE,legend.position="none")
I looked at the online ggplot2 doc and could not find anything there either....
If you just use the normal qplot command and then add + theme(legend.position = "none") to your code, it will remove the legend. So, your code will look as follows:
qplot(plotseries,geom="density",fill="red",na.rm=TRUE) + theme(legend.position="none")
Usually such things work just as they would work in the ggplot2 command.
I am new to using qplot and ggplot, and basically want to make a figure that is just the combination of a bar plot and a line plot. I can do one or the other, but don't know how to do both at once!
Here is my data:
bulk = data.frame(x_pos=c(1,2,3,4,5,6,7,8),
y_line=c(3,7,6,8,14,16,18,12),
y_bar=c(0,0,10,0,0,0,10,0))
For a line graph, I just do qplot(x_pos, y_line, data=bulk, geom="line")
For a bar plot, I just do qplot(x_pos, y_bar, data=bulk)
But! How can I combine these at once into a single figure?? My real intention is to use several (maybe 6-10) different graphics techniques like this to generate complex figures, but it all starts with knowing how to do two at once. Thanks for any help!
Don't use qplot for this.
library(ggplot2)
ggplot(bulk, aes(x=x_pos)) +
geom_bar(aes(y=y_bar), stat="identity") +
geom_line(aes(y=y_line), color="red", size=2)
Hi I am trying to look at different ways to visualize a dataset by using hexplots in ggplot.
I essentially want a hexplot with
1. loess line
2. regression line
3.x=y line --> equivalent of abline(0,1)
So far I have come up with this kind of code:
c <- ggplot(mtcars, aes(qsec, wt))
c+stat_binhex()+stat_smooth(method="loess", colour="red")+stat_smooth(method='lm', se=FALSE, colour="orange")+ geom_abline(intercept=0, slope=1)
This gives the picture below, but I still do not see the x=y reference line. Please help. I'm not sure why it is not working. Thanks
The y=x reference line is not inside the coordinate ranges you plot. If you change to
geom_abline(slope=1, intercept=-15)
you will see your line on the plot.
I'm trying to inset a plot using ggplot2 and annotation_custom (the plot is actually a map that I'm using to replace the legend). However, I'm also using facet_wrap to generate multiple panels, but when used with annotation_custom, this reproduces the plot in each facet. Is there an easy way to insert the plot only once, preferably outside the plotting area?
Here is a brief example:
#Generate fake data
set.seed(9)
df=data.frame(x=rnorm(100),y=rnorm(100),facets=rep(letters[1:2]),
colors=rep(c("red","blue"),each=50))
#Create base plot
p=ggplot(df,aes(x,y,col=colors))+geom_point()+facet_wrap(~facets)
#Create plot to replace legend
legend.plot=ggplotGrob(
ggplot(data=data.frame(colors=c("red","blue"),x=c(1,1),y=c(1,2)),
aes(x,y,shape=colors,col=colors))+geom_point(size=16)+
theme(legend.position="none") )
#Insert plot using annotation_custom
p+annotation_custom(legend.plot)+theme(legend.position="none")
#this puts plot on each facet!
This produces the following plot:
When I would like something more along the lines of:
Any help is appreciated. Thanks!
In the help of annotation_custom() it is said that annotations "are the same in every panel", so it is expected result to have your legend.plot in each facet (panel).
One solution is to add theme(legend.position="none") to your base plot and then use grid.arrange() (library gridExtra) to plot both plots.
library(gridExtra)
p=ggplot(df,aes(x,y,col=colors))+geom_point()+facet_wrap(~facets)+
theme(legend.position="none")
grid.arrange(p,legend.plot,ncol=2,widths=c(3/4,1/4))