Generate a Filled geom_step - r

I can get a "filled" geom_line with either geom_ribbon or geom_area. Is there an equivalent for geom_step that doesn't require messing with polygons/barplots or creating the actual step points? Here is some sample data:
library(ggplot2)
set.seed(1)
df <- data.frame(
x=rep(sort(sample(1:20, 5)), 3),
y=ave(runif(15), rep(1:3, each=5), FUN=cumsum),
grp=letters[rep(1:3, each=5)]
)
ggplot(df, aes(x=x, y=y, color=grp)) + geom_step(position="stack")
Which produces:
Basically, I want exactly the same thing, but with filled areas. I know how to do this by actually creating the x/y values required for the steps and using geom_area, but I'm hoping there is something simpler.

Here is the answer I was thinking of, for reference, but I'm hoping for something simpler/built-in if possible:
df2 <- rbind(
df,
transform(df[order(df$x),],
x=x - 1e-9, # required to avoid crazy steps
y=ave(y, grp, FUN=function(z) c(z[[1]], head(z, -1L)))
) )
ggplot(df2, aes(x=x, y=y, fill=grp)) + geom_area()

I know the question is a few years old, but I had the same problem today. For reference here's my solution. It's not more concise than the original answer, but may be easier to understand for some.
library(ggplot2)
library(dplyr)
df <- data.frame(x = seq(10), y = sample(10))
df_areaStep <- bind_rows(old = df,
new = df %>% mutate(y = lag(y)),
.id = "source") %>%
arrange(x, source)
ggplot(df, aes(x,y)) +
geom_ribbon(aes(x = x, ymin = 0, ymax = y), data = df_areaStep)
See this gist for longer version with comments.

Related

R plot x and y axes changes

I have my data:
x <- list(10,50,100,250,500,2500)
y <- list(0.09090909,0.01960784,0.003984064,0.003984064,0.001996008,0.0003998401)
I want to link my data to code found on this website
log_c <- seq(-12, 0, by = 0.1)
df <- data.frame(log_c = log_c, response = V(log_c, E, HS, log_EC50))
ggplot(df, aes(log_c, response)) +
geom_line()
I want to put the x data in place of log_c and y data in place of response. These are my beginnings in R, so I am asking for help
At a minimum this will work:
you have expressed your x and y values as lists; it will work better if they are (atomic) vectors (which you would have gotten using c() instead of list()). Put them into a data frame (not strictly necessary, but best practice):
df <- data.frame(x=unlist(x), y = unlist(y))
Use the data frame to draw your plot:
library(ggplot2)
ggplot(df, aes(x, y)) + geom_line()
To get your graph looking a little more like the example in your post,
scalefun <- function(y) (y-min(y))/diff(range(y))
ggplot(df, aes(x, y = scalefun(y))) + geom_line() + scale_x_log10()
Same logic as #Ben Bolker: Just another way:
library(tidyverse)
tibble(x,y) %>%
unnest() %>%
ggplot(aes(x, y))+
geom_line()

Symmetric y-axis limits for barchart in ggplot2

I would like to make the y-axis of a bar chart symmetric, so that it's easier to see if positive or negative changes are bigger. Since otherwise this is a bit distorted. I do have working code although it's a bit clumsy and I thought it would be great if I could directly do this in the first ggplot() call. So as to say that ylim directly is symmetrical.
set.seed(123)
my.plot <- ggplot( data = data.table(x = 1:10,
y = rnorm(10,0, 2)), aes(x=x, y=y)) +
geom_bar(stat="identity")
rangepull <- layer_scales(my.plot)$y
newrange <- max(abs(rangepull$range$range))
my.plot +
ylim(newrange*-1, newrange)
What about this :
library(ggplot2)
library(data.table)
set.seed(123)
my.data = data.table(x = 1:10, y = rnorm(10,0, 2))
my.plot <- ggplot(data = my.data)+aes(x=x, y=y) +
geom_bar(stat="identity")+ylim((0-abs(max(my.data$y))),(0+max(abs(my.data$y))))
my.plot
You may want to consider using ceiling:
set.seed(123)
library(ggplot2)
library(data.table)
dT <- data.table(x = 1:10, y = rnorm(10,0, 2))
my.plot <- ggplot(dT, aes(x=x, y=y)) +
geom_bar(stat="identity") +
ylim(-ceiling(max(abs(dT$y))), ceiling(max(abs(dT$y))))
This will give you:
> my.plot

ggplot bar plot side by side using two variables [duplicate]

This question already has an answer here:
How to create side-by-side bar charts (for multiple series) with ggplot?
(1 answer)
Closed 5 years ago.
I want to create a barplot using ggplot in R studio using two variables side by side. I tried following other people suggestions I found online, but I cant get it to work.
Here's the data I'm using:
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)
So, what I'm trying to do is have days on the x-axis and side by side barplots of x and y (with x & y being colored) corresponding to the day number.
First thing i did was make a data frame :
df1 <- data.frame(x,y,day)
and then I tried:
ggplot(df1, aes(x = day, y = x,y)) + geom_bar(stat = "identity",color = x, width = 1, position="dodge")
But I just can't get it to work properly. Any suggestions as to how I'd achieve this?
You have the right idea, I think the melt() function from the reshape2 package is what you're looking for.
library(ggplot2)
library(reshape2)
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)
df1 <- data.frame(x, y, day)
df2 <- melt(df1, id.vars='day')
head(df2)
ggplot(df2, aes(x=day, y=value, fill=variable)) +
geom_bar(stat='identity', position='dodge')
EDIT
I think the pivot_longer() function from the tidyverse tidyr package might now be the better way to handle these types of data manipulations. It gives quite a bit more control than melt() and there's also a pivot_wider() function as well to do the opposite.
library(ggplot2)
library(tidyr)
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)
df1 <- data.frame(x, y, day)
df2 <- tidyr::pivot_longer(df1, cols=c('x', 'y'), names_to='variable',
values_to="value")
head(df2)
ggplot(df2, aes(x=day, y=value, fill=variable)) +
geom_bar(stat='identity', position='dodge')
Or you could use facet_wrap to produce two plots:
library("ggplot2")
library("reshape")
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- c(1,2,3,4,5,6,7,8,9,10,11,12)
df1 <- data.frame(x,y,day)
df2 <- reshape::melt(df1, id = c("day"))
ggplot(data = df2, aes(x = day, y = value, fill = variable)) + geom_bar(stat = "identity")+ facet_wrap(~ variable) + scale_x_continuous(breaks=seq(1,12,2))
If you want the bars with color according to the day use fill = day:
ggplot(data = df2, aes(x = day, y = value, fill = day)) + geom_bar(stat = "identity") + facet_wrap(~ variable) + scale_x_continuous(breaks=seq(1,12,2))

Multiple curves in ggplot2 with same independent variable

I have a sequence of points in the x-axis for each of which there are two points in the y-axis.
x<-seq(8.5,10,by=0.1)
y<-c(0.9990276914, 0.9973015358, 0.9931704801, 0.9842176288, 0.9666471511, 0.9354201700, 0.8851624615, 0.8119131899, 0.7152339504, 0.5996777045, 0.4745986612, 0.3519940258, 0.2431610835, 0.1556738744, 0.0919857178, 0.0500000000, 0.0249347645, 0.0113838852, 0.0047497169, 0.0018085048, 0.0006276833)
y1<-c(9.999998e-01,9.999980e-01,9.999847e-01,9.999011e-01,9.994707e-01,9.976528e-01,9.913453e-01, 9.733730e-01, 9.313130e-01, 8.504646e-01, 7.228116e-01, 5.572501e-01,3.808638e-01,2.264990e-01, 1.155286e-01, 5.000000e-02, 1.821625e-02, 5.554031e-03, 1.410980e-03, 2.976926e-04, 5.203069e-05)
I would now like to create two curves in ggplot2. This is quite easy to accomplish in the normal way in R. The result is in the plot below. I am not sure, however, how to do that in ggplot2. For just one curve, I can use
library(ggplot2)
p<-qplot(x,y,geom="line")
Could you please help me generalise the above? Any help is greatly appreciated, thank you.
Note that the lengths of your x and y values don't match. Combine your data and use a grouping variable:
x<-seq(8.5,10, length.out = 21)
DF <- data.frame(x=rep(x, 2), y=c(y, y1), g=c(y^0, y1^0*2))
library(ggplot2)
ggplot(DF, aes(x=x, y=y, colour=factor(g), linetype=factor(g))) +
geom_line()
As #Roland also pointed out first you should fix the length of x. A possible solution using the reshape2 package:
library(reshape2)
library(ggplot2)
x<-seq(8.5,10,length.out = 21)
y<-c(0.9990276914, 0.9973015358, 0.9931704801, 0.9842176288, 0.9666471511, 0.9354201700, 0.8851624615, 0.8119131899, 0.7152339504, 0.5996777045, 0.4745986612, 0.3519940258, 0.2431610835, 0.1556738744, 0.0919857178, 0.0500000000, 0.0249347645, 0.0113838852, 0.0047497169, 0.0018085048, 0.0006276833)
y1<-c(9.999998e-01,9.999980e-01,9.999847e-01,9.999011e-01,9.994707e-01,9.976528e-01,9.913453e-01, 9.733730e-01, 9.313130e-01, 8.504646e-01, 7.228116e-01, 5.572501e-01,3.808638e-01,2.264990e-01, 1.155286e-01, 5.000000e-02, 1.821625e-02, 5.554031e-03, 1.410980e-03, 2.976926e-04, 5.203069e-05)
df <- data.frame(x, y, y1)
df <- melt(df, id.var='x')
ggplot(df, aes(x = x, y = value, color = variable))+geom_line()
EDIT:
Changing the linetype and legend:
g <- ggplot(df, aes(x = x, y = value, color = variable, linetype=variable)) + geom_line()
g <- g + scale_linetype_discrete(name="Custom legend name",
labels=c("Curve1", "Curve2"))
g <- g + guides(color=FALSE)
print(g)

Put whisker ends on boxplot

I would like to put perpendicular lines at the ends of the whiskers like the boxplot function automatically gives.
As hinted but not implemented by #Roland, you can use stat_boxplot to implement this. The trick calling _boxplot twice and is to set the geom to errorbar for one of the calls.
Note that as R uses a pen and paper approach it is advisable to implement the error bars first the draw the traditional boxplot over the top.
Using #Roland's dummy data df
ggplot(df, aes(x=cond, y = value)) +
stat_boxplot(geom ='errorbar') +
geom_boxplot() # shorthand for stat_boxplot(geom='boxplot')
The help for stat_boxplot (?stat_boxplot) detail the various values computed and saved in a data.frame
To resize the whiskers lines we can use the argument width = 0.5 inside the function: stat_boxplot
set.seed(42)
df <- data.frame(cond = factor(rep(c("A", "B"), each = 500)),
value = c(rnorm(500, mean = 1, sd = 0.2),
rnorm(500, mean = 1.5, sd = 0.1)))
library(ggplot2)
ggplot(df, aes(x = cond, y = value)) +
stat_boxplot(geom = "errorbar", width = 0.5) +
geom_boxplot()
It might be possible to use stat_boxplot to calculate the whisker ends, but I am not enough of a ggplot2 wizard, so I use the base function for that.
set.seed(42)
df <- data.frame(cond = factor( rep(c("A","B"), each=500) ),
value = c(rnorm(500,mean=1,sd=0.2),rnorm(500, mean=1.5,sd=0.1)))
whisk <- function(df,cond_col=1,val_col=2) {
require(reshape2)
condname <- names(df)[cond_col]
names(df)[cond_col] <- "cond"
names(df)[val_col] <- "value"
b <- boxplot(value~cond,data=df,plot=FALSE)
df2 <- cbind(as.data.frame(b$stats),c("min","lq","m","uq","max"))
names(df2) <- c(levels(df$cond),"pos")
df2 <- melt(df2,id="pos",variable.name="cond")
df2 <- dcast(df2,cond~pos)
names(df2)[1] <- condname
df2
}
library(ggplot2)
plot1 <- ggplot(df, aes(x=cond))
plot1 <- plot1 + geom_errorbar(aes(ymin=min,ymax=max),data=whisk(df),width = 0.5)
plot1 <- plot1 + geom_boxplot(aes(y=value))
plot1

Resources