Line plot error with ggplot2 and geom_line - r

I'm trying to run the code below which produces errors like:
In geom_line(stat = "summary", fun.y = mean) :
Ignoring unknown parameters: `fun.y`
ggplot(aes(x = age, y = friend_count), data = pf) +
coord_cartesian(xlim = c(13,90)) +
geom_point(alpha = 0.05,
position = position_jitter(h = 0),
color = 'orange') +
coord_trans(y = "sqrt") +
geom_line(stat = 'summary', fun.y = mean) +
geom_line(stat = 'summary', fun.y = quantile, fun.args = list(probs = .1)) +
geom_line(stat = 'summary', fun.y = quantile, fun.args = list(probs = .9))
RStudio's linter isn't happy about probs in the list() either.
I'd appreciate any help. All the googling I did led to this solution which isn't working.
Thanks in advance,
Brian

Related

How to add y value average text to geom_bar?

ggplot(aes(x=MALE, y=AMOUNT, fill=MALE)) + geom_bar(stat="summary", fun="mean") +
ylab("Avg Amount") + theme(axis.title.x = element_blank())
How can I add the y value to the top of the bars given I've already created stat='summary' & fun='mean' when I created the graph?
To add the y value as label on top of your bars you can do:
geom_text(aes(label = after_stat(y)), stat = "summary", fun = "mean", vjust = -.1)
Using mtcars as example data and with some additional formatting of the label:
library(ggplot2)
ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(cyl))) +
geom_bar(stat = "summary", fun = "mean") +
geom_text(aes(label = after_stat(sprintf("%.1f", y))), stat = "summary", fun = "mean", vjust = -.1) +
ylab("Avg Amount") +
theme(axis.title.x = element_blank())

Define and specify legend quantiles scatter plot R

I have eg data and syntax for a scatter (jitter) plot below
eg_data <- data.frame(
period = c(sample( c("1 + 2"), 1000, replace = TRUE)),
max_sales = c(sample( c(1,2,3,4,5,6,7,8,9,10), 1000, replace = TRUE, prob =
c(.20, .10, .15, .20, .15, .10, .05, .02, .02, .01))) )
jitter <- (
(ggplot(data = eg_data, aes(x=period, y=max_sales)) +
geom_jitter(stat = "identity", width = .15, color = "blue", alpha = .4)) +
scale_y_continuous(breaks= seq(0,12, by=1)) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.25)), geom = "hline", aes(yintercept = ..y..), colour = "red", size = 1) +
stat_summary(fun.y = "mean", geom = "hline", aes(yintercept = ..y..), colour = "gold", size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.50)), geom = "hline", aes(yintercept = ..y..), colour = "blue", size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.75)), geom = "hline", aes(yintercept = ..y..), colour = "black", size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.90)), geom = "hline", aes(yintercept = ..y..), colour = "green", size = 1) +
ggtitle("Max Sales x Period 1 and 2") + xlab("Period") + ylab("Sales") +
theme(plot.title = element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(color = "black", size = 12, face = "bold"),
axis.title.y = element_text(color = "black", size = 12, face = "bold")) +
labs(fill = "Period") )
jitter
I cannot find documentation on how to define a legend for the horiztonal quantile / mean lines I have in this graph.
How to add legend to ggplot manually? - R
I came across this SO question / answer but I wasn't able to implement it, when I include color inside the aes setting, it doesn't work.
EDIT - a member suggested I add color to the aes specification...here is the same graph with color and size included.
jitter2 <- (
(ggplot(data = eg_data, aes(x=period, y=max_sales)) +
geom_jitter(stat = "identity", width = .15, color = "blue", alpha = .4)) +
scale_y_continuous(breaks= seq(0,12, by=1)) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.25)), geom = "hline", aes(yintercept = ..y.., colour = "red"), size = 1) +
stat_summary(fun.y = "mean", geom = "hline", aes(yintercept = ..y.., colour = "gold"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.50)), geom = "hline", aes(yintercept = ..y.., colour = "blue"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.75)), geom = "hline", aes(yintercept = ..y.., colour = "black"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.90)), geom = "hline", aes(yintercept = ..y.., colour = "green"), size = 1) +
ggtitle("Max Sales x Period 1 and 2") + xlab("Period") + ylab("Sales") +
theme(plot.title = element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(color = "black", size = 12, face = "bold"),
axis.title.y = element_text(color = "black", size = 12, face = "bold")) +
labs(fill = "Period") )
jitter2
So...any help is appreciated. Thank you!
I found the answer to my own question.
jitter <- (
(ggplot(data = eg_data, aes(x=period, y=max_sales)) +
geom_jitter(stat = "identity", width = .15, color = "blue", alpha = .4)) +
scale_y_continuous(breaks= seq(0,12, by=1)) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.25)), geom = "hline", aes(yintercept = ..y.., colour = "25%"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.50)), geom = "hline", aes(yintercept = ..y.., colour = "50%"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.75)), geom = "hline", aes(yintercept = ..y.., colour = "75%"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.90)), geom = "hline", aes(yintercept = ..y.., colour = "90%"), size = 1) +
stat_summary(fun.y = "mean", geom = "hline", aes(yintercept = ..y.., colour = "mean"), size = 1.5) +
ggtitle("Max Sales x Period 1 and 2") + xlab("Period") + ylab("Sales") +
theme(plot.title = element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(color = "black", size = 12, face = "bold"),
axis.title.y = element_text(color = "black", size = 12, face = "bold")) +
scale_colour_manual(values = c("red", "blue", "gold", "green", "black"), name = "Percentiles"))
jitter
Also, quickly, the idea of "just use (something), everyone gets it" as a suggestion is not helpful, and assumes way too much about the final intended audience. First time I've ever posted a question and had that as a reply. I asked a specific question for a specific reason.

Adding significance levels in faceted plots with two results in the same x axis

I know that there are many questions already open to add significance level in ggplot2 in faceted plots. However, as a R beginner, I did not found a solution for my plot.
My data (.txt file) is available in this link:
1drv.ms/t/s!AsLAxDXdkA9Mg8oXdJ-qxD5AeB4KAw
There are four columns: three factor levels (temperature, parasitoid species and behavior) and a numeric level (number of parasitism and host-killing).
I run the plot with the code:
ggplot(mydata, aes(x = temperature, y = value, fill = species)) +
facet_grid(. ~ behavior) +
stat_summary(fun.y = mean, geom = "bar", position = "dodge", stat="identity") +
stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1) +
labs(x = "Temperature", y = "Nº of parasitized or host-killed larvae") +
theme(legend.position = "bottom", legend.title = element_blank(), legend.text = element_text(size = 11, face = "italic"))
Now I want to add the significance level in each plot, comparing separately the results in Parasitism and Host-Killing in both temperatures. So I have 6 contrasts in each plot. I tested the option using the function stat_compare_means. However, as advised in this Rblogger tutorial (https://www.r-bloggers.com/add-p-values-and-significance-levels-to-ggplots/) I should make a list telling what I want to compare. But, in this case, I have two results for 25°C and two for 30°C. How I could create this list mentioning all the contrasts?
If anybody could clarify for me how I can solve this, it would help me a lot. Thanks.
stat_compare_means worked "out of the box" (which makes sense, since it's using the grouping defined in the ggplot function.
ggplot(mydata, aes(x = temp, y = value, fill = factor(species))) +
facet_grid(. ~ behavior) +
stat_summary(fun.y = mean, geom = "bar", position = position_dodge(width = 1), stat="identity") +
stat_summary(fun.data = mean_sdl, geom="errorbar", position = position_dodge(width = 1), width=0.25, stat="identity", fun.args = list(mult = 1)) +
labs(x = "Temperature", y = "Nº of parasitized or host-killed larvae") +
theme(legend.position = "bottom", legend.title = element_blank(), legend.text = element_text(size = 11, face = "italic")) +
stat_compare_means(method = "t.test")
Alternatively, switch the x and fill variables:
ggplot(mydata, aes(x = factor(species), y = value, fill = factor(temp))) +
facet_grid(. ~ behavior) +
stat_summary(fun.y = mean, geom = "bar", position = position_dodge(width = 1), stat="identity") +
stat_summary(fun.data = mean_sdl, geom="errorbar", position = position_dodge(width = 1), width=0.25, stat="identity", fun.args = list(mult = 1)) +
labs(x = "Temperature", y = "Nº of parasitized or host-killed larvae") +
theme(legend.position = "bottom", legend.text = element_text(size = 11, face = "italic")) +
stat_compare_means(method = "t.test")
Thanks #thc for your help and patience. As I decided to use letters over the error bars, I had to create a new data.frame for each letter and them add the geom_text. This got it right with the below code, after testing many x and y values to insert the letters in the right point. But certainly it has an easier way to do it that I don't know it.
sigvals1 <- data.frame(x=0.75,y=222.4, text=c("a"), behavior=c("Parasitism"))
sigvals2 <- data.frame(x=1.207,y=190.55, text=c("a"), behavior=c("Parasitism"))
sigvals3 <- data.frame(x=1.75,y=117.7, text=c("b"), behavior=c("Parasitism"))
sigvals4 <- data.frame(x=2.209,y=103, text=c("b"), behavior=c("Parasitism"))
sigvals5 <-data.frame(x = 0.75, y = 74.8, text=c("A"), behavior = c("Host-Killing"))
sigvals6 <-data.frame(x = 1.20, y = 92.97, text=c("B"), behavior = c("Host-Killing"))
sigvals7 <-data.frame(x = 1.74, y = 49.8, text=c("C"), behavior = c("Host-Killing"))
sigvals8 <-data.frame(x = 2.196, y = 43, text=c("C"), behavior = c("Host-Killing"))
plot +
geom_text(data=sigvals1, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals2, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals3, aes(x=x,y=y, label=text, fill=NA), hjust=0)+
geom_text(data=sigvals4, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals5, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals6, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals7, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals8, aes(x=x,y=y, label=text, fill=NA), hjust=0)

How to do the equivalent of "dodge" and "nudge" at the same time in ggplot2, R [duplicate]

I have a ggplot plot. I need to shift error bars relative to jittered points. My code is:
data("cabbages", package = "MASS")
require("ggplot2")
pos_1 <- position_jitterdodge(
jitter.width = 0.25,
jitter.height = 0,
dodge.width = 0.9
)
gg <-
ggplot(data = cabbages,
aes(
x = Cult,
y = HeadWt,
colour = Cult,
fill = Cult
)) +
geom_jitter(alpha = 0.4, position = pos_1) +
stat_summary(fun.y = "mean", geom = "point", size = 3) +
stat_summary(fun.data = "mean_cl_normal",
geom = "errorbar",
width = 0.05,
lwd = 1,
fun.args = list(conf.int = 0.95)
) +
theme_bw()
print(gg)
Current result is:
And I need something like this:
You may add an offset to x in aes in eachstat_summary (aes(x = as.numeric(Cult) + 0.2)):
ggplot(data = cabbages,
aes(x = Cult,
y = HeadWt,
colour = Cult,
fill = Cult)) +
geom_jitter(alpha = 0.4, position = pos_1) +
stat_summary(aes(x = as.numeric(Cult) + 0.2), fun.y = "mean", geom = "point", size = 3) +
stat_summary(aes(x = as.numeric(Cult) + 0.2), fun.data = "mean_cl_normal",
geom = "errorbar",
width = 0.05,
lwd = 1,
fun.args = list(conf.int = 0.95)) +
theme_bw()
Nowadays you can use position_nudge() with the same effect:
stat_summary(
fun.y = "mean",
geom = "point",
size = 3,
position = position_nudge(x=0.2)
) +
stat_summary(
fun.data = "mean_cl_normal",
geom = "errorbar",
width = 0.05,
lwd = 1,
fun.args = list(conf.int = 0.95),
position = position_nudge(x=0.2)
)

Interaction Plot in ggplot2

I'm trying to make interaction plot with ggplot2. My code is below:
library(ggplot2)
p <- qplot(as.factor(dose), len, data=ToothGrowth, geom = "boxplot", color = supp) + theme_bw()
p <- p + labs(x="Dose", y="Response")
p <- p + stat_summary(fun.y = mean, geom = "point", color = "blue")
p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = 1))
p <- p + opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0))
p <- p + opts(axis.title.y = theme_text(size = 12, angle = 90, vjust = 0.25))
print(p)
How can I plot dose-supp level combination means rather than only dose level means which I'm getting here? Thanks in advance for your help.
You can precalculate the values in their own data frame:
toothInt <- ddply(ToothGrowth,.(dose,supp),summarise, val = mean(len))
ggplot(ToothGrowth, aes(x = factor(dose), y = len, colour = supp)) +
geom_boxplot() +
geom_point(data = toothInt, aes(y = val)) +
geom_line(data = toothInt, aes(y = val, group = supp)) +
theme_bw()
Note that using ggplot rather than qplot makes the graph construction a lot clearer for more complex plots like these (IMHO).
You can compute your summaries by the appropriate groups (supp):
p <- qplot(as.factor(dose), len, data=ToothGrowth, geom = "boxplot", color = supp) + theme_bw()
p <- p + labs(x="Dose", y="Response")
p <- p + stat_summary(fun.y = mean, geom = "point", color = "blue", aes(group=supp))
p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = supp))
p <- p + opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0))
p <- p + opts(axis.title.y = theme_text(size = 12, angle = 90, vjust = 0.25))
print(p)
Or converting to ggplot syntax (and combining into one expression)
ggplot(ToothGrowth, aes(as.factor(dose), len, colour=supp)) +
geom_boxplot() +
stat_summary(aes(group=supp), fun.y = mean, geom="point", colour="blue") +
stat_summary(aes(group=supp), fun.y = mean, geom="line") +
scale_x_discrete("Dose") +
scale_y_continuous("Response") +
theme_bw() +
opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0),
axis.title.y = theme_text(size = 12, angle = 90, vjust = 0.25))
EDIT:
To make this work with 0.9.3, it effectively becomes Joran's answer.
library("plyr")
summ <- ddply(ToothGrowth, .(supp, dose), summarise, len = mean(len))
ggplot(ToothGrowth, aes(as.factor(dose), len, colour=supp)) +
geom_boxplot() +
geom_point(data = summ, aes(group=supp), colour="blue",
position = position_dodge(width=0.75)) +
geom_line(data = summ, aes(group=supp),
position = position_dodge(width=0.75)) +
scale_x_discrete("Dose") +
scale_y_continuous("Response") +
theme_bw() +
theme(axis.title.x = element_text(size = 12, hjust = 0.54, vjust = 0),
axis.title.y = element_text(size = 12, angle = 90, vjust = 0.25))
If you think you might need a more general approach, you could try function rxnNorm in package HandyStuff (github.com/bryanhanson/HandyStuff). Disclaimer: I'm the author. Disclaimer #2: the box plot option doesn't quite work right, but all the other options are fine.
Here's an example using the ToothGrowth data:
p <- rxnNorm(data = ToothGrowth, res = "len", fac1 = "dose", fac2 = "supp", freckles = TRUE, method = "iqr", fac2cols = c("red", "green"))
print(p)
a much easier way. without ddply. directly with ggplot2.
ggplot(ToothGrowth, aes(x = factor(dose) , y=len , group = supp, color = supp)) +
geom_boxplot() +
geom_smooth(method = lm, se=F) +
xlab("dose") +
ylab("len")

Resources