ggplot2 fails to draw curved line - r

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)

Related

remove connecting lines when using color aesthetic ggplot in r

I'm plotting a time series with gaps from one source filled in from a second. Plotting this to indicate the source, I specify the source as a color aesthetic, but ggplot adds connecting lines tieing the gaps together.
Is there a clean way to remove these connecting lines? Ideally, I would like the separate groups to be connected, since I am using it as one data set.
library(ggplot)
set.seed(914)
df=data.frame(x=c(1:100),y=runif(100), z = rep(c("a", "b"), each = 25, times = 2))
ggplot(df, aes(x=x, y = y, color = z))+
geom_line()
Removing connetion lines between missing Dates in ggplot
suggests creating a group aesthetic, or making the gaps explicit using NA values.
I don't have any clear grouping aesthetic in my real data, like the year in the referenced example, and with many irregularly spaced gaps, it isn't immediately clear how to insert NA's in every gap.
As long as your colored "groups" are sequential in your data frame (as in your example) you can do:
ggplot(df, aes(x=x, y = y, color = z,
group = factor(c(0, cumsum(abs(head(z, -1) != tail(z, -1))))))) +
geom_line()
Or, for brevity, use data.table::rleid:
ggplot(df, aes(x=x, y = y, color = z, group = data.table::rleid(z))) +
geom_line()
which gives the same result

How to make a circled bubble plot using ggplot2 coord_polar()?

I have an example data, which does not have x- and y-axis information. I would like to make a bubble plot using R package ggplot2, and arrange the bubbles in a circled manner.
data <- data.frame(group = paste("Group", letters[1:11]),
value = sample(seq(1,100),11))
Thanks a lot.
You can just put a dummy value for y and make group your x values in aes.
ggplot(data, aes(x = group, y = 0, size = value)) +
coord_polar() +
geom_point()

move ggplot2 contour from other facets to main

I have x,y,z data with categorical variables that facilitate a facet. I want to include contour lines from all but the first facet and discard the rest of the data. One way to visualize the process is to facet the data and mentally move the contours from the other facets to the first.
MWE:
library(ggplot2)
library(dplyr)
data(volcano)
nx <- 87; ny <- 61
vdat <- data_frame(w=0L, x=rep(seq_len(nx), ny), y=rep(seq_len(ny), each=nx), z=c(volcano))
vdat <- bind_rows(vdat,
mutate(vdat, w=1L, x=x+4, y=y+4, z=z-20),
mutate(vdat, w=2L, x=x+8, y=y+8, z=z-40))
ggplot(vdat, aes(x, y, fill=z)) +
geom_tile() +
facet_wrap(~ w, nrow=1) +
geom_contour(aes(z=z), color='white', breaks=c(-Inf,110,Inf))
In each facet, I have:
facet 0: X,Y,Z for w==0L, contour for w==0L
facet 1: X,Y,Z for w==1L, contour for w==1L
facet 2: X,Y,Z for w==2L, contour for w==2L
What I'd like to have is a single pane, effectively:
X,Y,Z for w==0L, contour for all values of the w categorical
(Forgive my hasty GIMP skills. In the real data, the contours will likely not overlap, but I don't think that that would be a problem.)
The real data has different values (and gradients) of z for the same X,Y system, so the contour is otherwise compatible with the first facet. However, it's still "different", so I cannot mock-up the contours with the single w==0L data.
I imagine there might be a few ways to do this:
form the data "right" the first time, informing ggplot how to pull the contours but lay them on the single plot (e.g., using different data= for certain layers);
form the faceted plot, extract the contours from the other facets, apply them to the first, and discard the other facets (perhaps using grid and/or gtable); or perhaps
(mathematically calculate the contours myself and add them as independent lines; I was hoping to re-use ggplot2's efforts to avoid this ...).
It doesn't fit so neatly with the grammar of graphics, but you can just add a geom_contour call for each subset of data. A quick way is to add a list of such calls to the graph, which you can generate quickly by lapplying across the split data:
ggplot(vdat[vdat$w == 0, ], aes(x, y, z = z, fill = z)) +
geom_tile() +
lapply(split(vdat, vdat$w), function(dat){
geom_contour(data = dat, color = 'white', breaks = c(-Inf, 110, Inf))
})
You can even make a legend, if you need:
ggplot(vdat[vdat$w == 0, ], aes(x, y, z = z, fill = z, color = factor(w))) +
geom_raster() +
lapply(split(vdat, vdat$w), function(dat){
geom_contour(data = dat, breaks = c(-Inf, 110, Inf))
})

R ggplot how to overlay partial graph on the full graph

Is there a way to overlay partial graph on top of full graph using ggplot? I have one line graph with time span of say 100 days on X axis and need to add second line that only spans last 20 days, with different color; I don't want to plot second line as having zero values for first 80 days - need to only plot it for last 20 days- using different color. What is the best way to do that?
Sure, just use two geoms with different subsets of your data.frame (for simplicity I use the full df and only one subset):
library(ggplot2)
df <- data.frame(Index = 1:1000, Value = cumsum(rnorm(1000)))
ggplot() + geom_line(data = df, aes(x = Index, y = Value)) +
geom_line(data = df[500:700,], aes(x = Index, y = Value), col="red")

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