Plotting density plots on the negative y axis with ggplot2, R - r

I'm trying to plot densities on both sides of the x axis - trying to visualize reads mapping to both strands of a genomic region.
I have two problems :
I want to use ggplot2 but
I don't know how to extract the data from the plot object, or invert the plot that has already been made.
Any help greatly appreciated!

Your question is very unclear, but I found the "densities on both sides of the x axis" potentially interesting. So maybe you're trying to do something like this:
d <- data.frame(x = rnorm(1000),x1 = rnorm(1000,sd = 0.5))
ggplot(data = d,aes(x = x)) +
geom_density() +
geom_density(aes(x = x1,y = -(..density..)))

Related

R: plotting a line and horizontal barplot on the same plot

I am trying to combine a line plot and horizontal barplot on the same plot. The difficult part is that the barplot is actually counts of the y values of the line plot.
Can someone show me how this can be done using the example below ?
library(ggplot2)
library(plyr)
x <- c(1:100)
dff <- data.frame(x = x,y1 = sample(-500:500,size=length(x),replace=T), y2 = sample(3:20,size=length(x),replace=T))
counts <- ddply(dff, ~ y1, summarize, y2 = sum(y2))
# line plot
ggplot(data=dff) + geom_line(aes(x=x,y=y1))
# bar plot
ggplot() + geom_bar(data=counts,aes(x=y1,y=y2),stat="identity")
I believe what I need is presented in the pseudocode below but I do not know how to write it out in R.
Apologies. I actually meant the secondary x axis representing the value of counts for the barplot, while primary y-axis is the y1.
ggplot(data=dff) + geom_line(aes(x=x,y=y1)) + geom_bar(data=counts , aes(primary y axis = y1,secondary x axis =y2),stat="identity")
I just want the barplots to be plotted horizontally, so I tried the code below which flip both the line chart and barplot, which is also not I wanted.
ggplot(data=dff) +
geom_line(aes(x=x,y=y1)) +
geom_bar(data=counts,aes(x=y2,y=y1),stat="identity") + coord_flip()
You can combine two plots in ggplot like you want by specifying different data = arguments in each geom_ layer (and none in the original ggplot() call).
ggplot() +
geom_line(data=dff, aes(x=x,y=y1)) +
geom_bar(data=counts,aes(x=y1,y=y2),stat="identity")
The following plot is the result. However, since x and y1 have different ranges, are you sure this is what you want?
Perhaps you want y1 on the vertical axis for both plots. Something like this works:
ggplot() +
geom_line(data=dff, aes(x=y1 ,y = x)) +
geom_bar(data=counts,aes(x=y1,y=y2),stat="identity", color = "red") +
coord_flip()
Maybe you are looking for this. Ans based on your last code you look for a double axis. So using dplyr you can store the counts in the same dataframe and then plot all variables. Here the code:
library(ggplot2)
library(dplyr)
#Data
x <- c(1:100)
dff <- data.frame(x = x,y1 = sample(-500:500,size=length(x),replace=T), y2 = sample(3:20,size=length(x),replace=T))
#Code
dff %>% group_by(y1) %>% mutate(Counts=sum(y2)) -> dff2
#Scale factor
sf <- max(dff2$y1)/max(dff2$Counts)
# Plot
ggplot(data=dff2)+
geom_line(aes(x=x,y=y1),color='blue',size=1)+
geom_bar(stat='identity',aes(x=x,y=Counts*sf),fill='tomato',color='black')+
scale_y_continuous(name="y1", sec.axis = sec_axis(~./sf, name="Counts"))
Output:

Symmetrical histograms

I want to make a number of symmetrical histograms to show butterfly abundance through time. Here's a site that shows the form of the graphs I am trying to create: http://thebirdguide.com/pelagics/bar_chart.htm
For ease, I will use the iris dataset.
library(ggplot2)
g <- ggplot(iris, aes(Sepal.Width)) + geom_histogram(binwidth=.5)
g + coord_fixed(ratio = .003)
Essentially, I would like to mirror this histogram below the x-axis. Another way of thinking about the problem is to create a horizontal violin diagram with distinct bins. I've looked at the plotrix package and the ggplot2 documentation but don't find a solution in either place. I prefer to use ggplot2 but other solutions in base R, lattice or other packages will be fine.
Without your exact data, I can only provide an approximate coding solution, but it is a start for you (if you add more details, I'll be happy to help you tweak the plot). Here's the code:
library(ggplot2)
noSpp <- 3
nTime <- 10
d <- data.frame(
JulianDate = rep(1:nTime , times = noSpp),
sppAbundance = c(c(1:5, 5:1),
c(3:5, 5:1, 1:2),
c(5:1, 1:5)),
yDummy = 1,
sppName = rep(letters[1:noSpp], each = nTime))
ggplot(data = d, aes(x = JulianDate, y = yDummy, size = sppAbundance)) +
geom_line() + facet_grid( sppName ~ . ) + ylab("Species") +
xlab("Julian Date")
And here's the figure.

How to plot points around a circle in R

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?

ggplot2 fails to draw curved line

I am trying to draw a curved line in ggplot2 which should look like this:
However, in ggplot2 I can only draw in the line in the following way:
Here is the code that I have used to create both pictures:
df1 <- data.frame(dollar = c(0,5,10,20,30), value = c(0,200,300, -100, -300))
# draw line graph with base plot
plot(y = df1$dollar, x = df1$emiss_red, type = "l")
# draw line graph with ggplot
ggplot() + geom_line(data = df1, aes(y = dollar, x = value), size =1)
Ggplot2 seems to order the data frame according to x value and then connect the points according to the x-value. However, I do not want my graph to be ordered.
Additionally, I do not want to flip the axis around, since dollar value must appear on the y-axis. Since I prefer to create these graphs in ggplot2, does anyone know how to accomplish this?
You just need to swap geom_line to geom_path. As noted in the documentation, geom_path connects "observations in original order", while geom_line connects "observations, ordered by x value".
So the last line would be
ggplot() + geom_path(data = df1, aes(y = dollar, x = value), size =1)

Line plot that changes color over "time"

I have a data frame that contains x and y coordinates for a random walk that moves in discrete steps (1 step up, down, left, or right). I'd like to plot the path---the points connected by a line. This is easy, of course. The difficulty is that the path crosses over itself and becomes difficult to interpret. I add jitter to the points to avoid overplotting, but it doesn't help distinguish the ordering of the walk.
I'd like to connect the points using a line that changes color over "time" (steps) according to a thermometer-like color scale.
My random walk is stored in its own class and I'm writing a specific plot method for it, so if you have suggestions for how I can do this using plot, that would be great. Thanks!
This is pretty easy to do in ggplot2:
so <- data.frame(x = 1:10,y = 1:10,col = 1:10)
ggplot(so,aes(x = x, y = y)) +
geom_line(aes(group = 1,colour = col))
If you prefer not to use ggplot, then ?segments will do what you want. -- I'm assuming here that x and y are both functions of time, as implied in your example.
If you use ggplot, you can set the colour aesthetic:
library(ggplot2)
walk <-cumsum(rnorm(n=100, mean=0))
dat <- data.frame(x = seq_len(length(walk)), y = walk)
ggplot(dat, aes(x,y, colour = x)) + geom_line()

Resources