I'm using this link to create the circle: Draw a circle with ggplot2
However, when I try to add my next ggplot which uses data from a CSV, I get two separate graphs. I'd like to have the circle overlay the scatterplot.
ggplot(CSV1, aes(x= Pos.X..µm., y = Pos.Y..µm.)) +
geom_point()
ggplot(dat, aes(x,y)) + geom_path()
Thanks!
You just need to combine it into a single object.
ggplot(CSV1, aes(x= Pos.X..µm., y = Pos.Y..µm.)) +
geom_point() +
geom_path(aes(x,y), data=dat)
Related
I have couple of questions regarding plotting using ggplot2.
I have already used below commands to colour data points using R.
library(ggplot2)
df <- read.csv(file="c:\\query2.csv")
ggplot( df,aes( x = Time,y ,y = users,colour = users>40) ) + geom_point()
My question is: how should I draw a continuous line connecting data points and how do I circle around data points for users >40?
To connect the points, use geom_line (if that doesn't give you what you need, please explain what you're trying to accomplish).
I haven't used geom_encircle, but another option is to use a filled marker with the fill deleted to create the circles. Here's an example, using the built-in mtcars data frame for illustration:
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_point(data=mtcars[mtcars$mpg>30,],
pch=21, fill=NA, size=4, colour="red", stroke=1) +
theme_bw()
pch=21 is one of the filled markers (see ?pch for more info on other available point markers). We set fill=NA to remove the fill. stroke sets the thickness of the circle border.
UPDATE: To add a line to this chart, using the example above:
ggplot(mtcars, aes(wt, mpg)) +
geom_line() +
geom_point() +
geom_point(data=mtcars[mtcars$mpg>30,],
pch=21, fill=NA, size=4, colour="red", stroke=1) +
theme_bw()
However, if (as in my original code for this graph) you put the aes statement inside the geom, rather than in the initial call to ggplot, then you need to include an aes statement inside geom_line as well.
I am trying to fill in a portion of a plot underneath a geom_smooth() line.
Example:
In the example the data fits on that curve. My data is not as smooth. I want to use geom_point() and a mix of geom_smooth() and geom_area() to fill in the area under the smoothed line while leaving the points above.
A picture of my data with a geom_smooth():
In other words, I want everything underneath that line to be filled in, like in Image 1.
Use predict with the type of smoothing being used. geom_smooth uses loess for n < 1000 and gam for n > 1000.
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth() +
geom_ribbon(aes(ymin = 0,ymax = predict(loess(hwy ~ displ))),
alpha = 0.3,fill = 'green')
Which gives:
I'm struggling to overlap rotated density plot onto the original scatterplot. Here are 2 plots I have:
require(ggplot2); set.seed(1);
df1 <- data.frame(ID=paste0('ID',1:1000), value=rnorm(1000,500,100))
p1 <- ggplot(data = df1, aes(x=reorder(ID, value), y=value)) +
geom_point(size=2, alpha = 0.7)+
coord_trans(y="log10")
p2 <- ggplot(data = df1, aes(x=value)) +
coord_trans(x="log10") +
geom_density() +
coord_flip()
p1
p2
First, there's a little problem with the density plot that its vertical axis is not log10-transformed. But main issue is that I can't find how to draw it on the previous plot keeping correct coordinates.
Because you are using coord_flip on your second plot you are effectively trying to plot two different values onto the same x axis (density and ID). There are plenty of posts discouraging this, here's one for example: How do I plot points with two different y-axis ranges on the same panel in the same X axis?.
I use ggplot2::ggplot for all 2D plotting needs, including density plots, but I find that when plotting a number of overlapping densities with extreme outliers on a single space (in different colors) the line on the x-axis becomes a little distracting.
My question is then, can you remove the bottom section of the density plot from being plotted? If so, how?
You can use this example:
library(ggplot2)
ggplot(movies, aes(x = rating)) + geom_density()
Should turn out like this:
How about using stat_density directly
ggplot(movies, aes(x = rating)) + stat_density(geom="line")
You can just draw a white line over it:
ggplot(movies, aes(x = rating)) +
geom_density() +
geom_hline(color = "white", yintercept = 0)
I am trying to plot some data on directions which vary from 0 to360 deg. The most intuitive way of doing this is around a circle where I can plot each point (I only have 13 points to plot).
cont=c(319,124,182,137,55,302,221,25,8,36,132,179,152)
My data for one plot
I tried following the ggplot2 guides and have not got it to work. I'm not very good at ggplot though...
(my dataframe is called "data")
ggplot(data, aes(x=1), ) + coord_polar(theta = "y") +geom_point(y=cont)
It works adding y to the ggplot mapping
data <- data.frame(cont = cont)
ggplot(data, aes(x=1, y = cont)) + coord_polar(theta = "y") + geom_point()
You can add other ggplot parameters to improve the appearence.
Have you tried polar.plot from plotrix library?