I have the following figure in R, generated using the ggplot2 package which resulted in the following:
The code to obtain this plot is:
df <- data.frame(value_x = c(10,20,30,40), value_y = c(89.3, 89.4, 89.60, 90.1))
myplot <- ggplot(data = df, aes(x = value_x, y = value_y)) +
geom_point() +
geom_line()
myplot
Now I want to fill the area under this curve, but still keep the y-axis scale.
When I add geom_area(alpha = 0.40) to the code, the plot becomes the following:
As you can see, the area runs from 0 to the curve, which rescales the y-axis. How can I inhibit this from happening?
I suggest the use of geom_ribbon which understands ymin and ymax aesthetics. Unlike geom_area which gives a continuous bar plot that starts at 0.
myplot +
geom_ribbon(aes(ymin = min(value_y), ymax = value_y))
Related
I want to plot a segmented bar plot in ggplot2. Here is part of my dataframe, I want to plot the proportion of output(0 and 1) for each x1(0 and 1). But when I use the following code, what I plot is just black bars without any segmentation. What's the problem in here?
fig = ggplot(data=df, mapping=aes(x=x1, fill=output)) + geom_bar(stat="count", width=0.5, position='fill')
The output plot is here
You need factor variables for your task:
library(ggplot2)
df <- data.frame(x1=sample(0:1,100,replace = T),output=sample(0:1,100,replace = T))
ggplot(data = df, aes(x = as.factor(x1), fill = as.factor(output))) +
geom_histogram(stat = "count")+
labs(x="x11")
which give me:
i'm new at R programming , how can i add a second y axis after plotting histogram so to plot geom_line
here's the code to plot histogram and geom_line, how can i solve my problem
I want to add a second axe y that can take into account the probability
library(ggplot2)
ggplot(x2, aes(x=X2, fill=as.factor(X1))) +
geom_histogram(alpha = 0.5,bins=100, position="dodge") +
geom_line(x2,mapping = aes(x = X2, y = probability))+
coord_cartesian(ylim = c(0, 150),xlim=c(0,20000000))+
xlab("Intensity")
Thank you
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()
I have two data frames: one I am using to create the bars in a barchart and a second that I am using to create a shaded "target region" behind the bars using geom_rect.
Here is example data:
test.data <- data.frame(crop=c("A","B","C"), mean=c(6,4,12))
target.data <- data.frame(crop=c("ONE","TWO"), mean=c(31,12), min=c(24,9), max=c(36,14))
I start with the means of test.data for the bars and means of target.data for the line in the target region:
library(ggplot2)
a <- ggplot(test.data, aes(y=mean, x=crop)) + geom_hline(aes(yintercept = mean, color = crop), target.data) + geom_bar(stat="identity")
a
So far so good, but then when I try to add a shaded region to display the min-max range of target.data, there is an issue. The shaded region appears just fine, but somehow, the crops from target.data are getting added to the x-axis. I'm not sure why this is happening.
b <- a + geom_rect(aes(xmin=-Inf, xmax=Inf, ymin=min, ymax=max, fill = crop), data = target.data, alpha = 0.5)
b
How can I add the geom_rect shapes without adding those extra names to the x-axis of the bar-chart?
This is a solution to your question, but I'd like to better understand you problem because we might be able to make a more interpretable plot. All you have to do is add aes(x = NULL) to your geom_rect() call. I took the liberty to change the variable 'crop' in add.data to 'brop' to minimize any confusion.
test.data <- data.frame(crop=c("A","B","C"), mean=c(6,4,12))
add.data <- data.frame(brop=c("ONE","TWO"), mean=c(31,12), min=c(24,9), max=c(36,14))
ggplot(test.data, aes(y=mean, x=crop)) +
geom_hline(data = add.data, aes(yintercept = mean, color = brop)) +
geom_bar(stat="identity") +
geom_rect(data = add.data, aes(xmin=-Inf, xmax=Inf, x = NULL, ymin=min, ymax=max, fill = brop),
alpha = 0.5, show.legend = F)
In ggplot calls all of the aesthetics or aes() are inherited from the intial call:
ggplot(data, aes(x=foo, y=bar)).
That means that regardless of what layers I add on geom_rect(), geom_hline(), etc. ggplot is looking for 'foo' to assign to x and 'bar' to assign to y, unless you specifically tell it otherwise. So like aeosmith pointed out you can clear all inherited aethesitcs for a layer with inherit.aes = FALSE, or you can knock out single variables at a time by reassigning them as NULL.
I tried adding a little summary table to a plot which I created with ggplot2::ggplot(). The table is added via gridExtra::tableGrob() to the saved ggplot object.
My problem is that this seems to change the y-limits of my original plot.
Is there a way to avoid that without having to specify the limits again via ylim()?
Here is a minimal example for the problem using the ChickWeight dataset:
# load packages
require(ggplot2)
require(gridExtra)
# create plot
plot1 = ggplot(data = ChickWeight, aes(x = Time, y = weight, color = Diet)) +
stat_summary(fun.data = "mean_cl_boot", size = 1, alpha = .5)
plot1
# create table to add to the plot
sum_table = aggregate(ChickWeight$weight,
by=list(ChickWeight$Diet),
FUN = mean)
names(sum_table) = c('Diet', 'Mean')
sum_table = tableGrob(sum_table)
# insert table into plot
plot1 + annotation_custom(sum_table)
EDIT:
I just figured out that it seems to be an issue with stat_summary(). When I use another geom/layer, then the limits stay as they were in the original plot. Another example for that:
plot2 = ggplot(data = ChickWeight, aes(x = Time, y = weight, color = Diet)) +
geom_jitter()
plot2
plot2 + annotation_custom(sum_table)
The y-range for plot1 is different from plot2, the reason being that annotation_custom takes its aesthetics from the original aes statement, not the modified data frame used by stat_summary(). To get the y-ranges for the two plots to be the same (or roughly the same - see below), stop annotation_custom getting its aesthetics from the original data. That is, move aes() inside the stat_summary().
# load packages
require(ggplot2)
require(gridExtra)
# create plot
plot1 = ggplot(data = ChickWeight) +
stat_summary(aes(x = Time, y = weight, color = Diet), fun.data = "mean_cl_boot", size = 1, alpha = .5)
plot1
# create table to add to the plot
sum_table = aggregate(ChickWeight$weight,
by=list(ChickWeight$Diet),
FUN = mean)
names(sum_table) = c('Diet', 'Mean')
sum_table = tableGrob(sum_table)
# insert table into plot
plot2 = plot1 + annotation_custom(sum_table, xmin = 10, xmax = 10, ymin = 200, ymax = 200)
plot2
By the way, the reason the two plots will not give the exact same y-range is because of the bootstrap function in stat_summary(). Indeed, plot p1 repeatedly, and you might notice slight changes in the y-range. Or check the y-ranges in the build data.
Edit Updating to ggplot2 ver 3.0.0
ggplot_build(plot1)$layout$panel_params[[1]]$y.range
ggplot_build(plot2)$layout$panel_params[[1]]$y.range
Recall that ggplot does not evaluate functions until drawing time - each time p1 or p2 is drawn, a new bootstrap sample is selected.