  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # Plotting the means in ggplot, without using stat_summary()  » r » Plotting the means in ggplot, without using stat_summary()

By : user2173165
Date : October 21 2020, 08:10 PM
this one helps. In ggplot, I want to compute the means (per group) and plot them as points. I would like to do that with geom_point(), and not stat_summary(). Here are my data. , You could do code :
``````ggplot(df, aes(group, grade)) +
geom_point(stat = 'summary', fun.y="mean")
``````
``````df %>% group_by(group) %>%
geom_point()
`````` ## plotting the means with confidence intervals with ggplot

By : Severin
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Since you have replicated data, and you want to plot mean/CL, you are probably better off using stat_summary(...) which is designed for (you guessed it) summarizing data. Basically, it applies a function to all the y-values for each x-value (so, the mean(...) function for example), and then plots the result using whatever geometry you specify. Here's an example:
code :
``````# sample data - should be provided in question
set.seed(1)      # for reproducible example
time <- 1:25
df   <- data.frame(time,
pop=rnorm(100*length(time), mean=10*time/(25+time)))

library(ggplot2)
ggplot(df, aes(x=time, y=pop))+
stat_summary(geom="ribbon", fun.data=mean_cl_normal, width=0.1, conf.int=0.95, fill="lightblue")+
stat_summary(geom="line", fun.y=mean, linetype="dashed")+
stat_summary(geom="point", fun.y=mean, color="red")
``````
``````ggplot(df, aes(x=time, y=pop))+
stat_summary(geom="ribbon", fun.data=mean_cl_normal,
fun.args=list(conf.int=0.95), fill="lightblue")+
stat_summary(geom="line", fun.y=mean, linetype="dashed")+
stat_summary(geom="point", fun.y=mean, color="red")
`````` ## Automatic n plotting with ggplot and stat_summary

By : Whoosh
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Well, as the help ?position_dodge states: Dodging things with different widths can be tricky. You may need to explicitly specify the width for dodging. In your case:
code :
``````ggplot(mtcars, aes(x=factor(cyl), mpg, fill=factor(am))) +
stat_summary(fun.data = n_fun, geom = "text",
position = position_dodge(.9))
`````` ## Error in stat_summary(fun.y) when plotting outliers in a modified ggplot-boxplot

By : Shamsa Almaawali
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The problem, as identified by @Heroka and @bdemarest, arose by one factor level having only one value.
My workaround is to skip those factors:
code :
``````f1 <- function(x) {
if (length(x) > 7) {
return(subset(x, x < quantile(x, probs=0.025))) # only for low outliers
} else {
return(NA)
}
}
`````` ## Plotting all data as geom_point and including lines showing means in ggplot2; issues with stat_summary

By : Chandu K
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You may need to generate the summaries before you start to avoid the grouping issue. One option is:
code :
``````library(dplyr)
summaryData <-
df %>%
group_by(StimulusType, Condition) %>%
summarise(meanRating = mean(Rating)
, jitterVal = mean(jitterVal)) %>%
mutate(xmin = StimulusType+jitterVal-0.04
, xend = StimulusType+jitterVal+0.04)

ggplot(df, aes(x=StimulusType+jitterVal, y=Rating, group=ParticipantCondition)) +
geom_point(size=4.5, aes(colour=Condition), alpha=0.3)+
geom_line(size=1, alpha=0.05)+
scale_y_continuous(limits=c(0, 7.5), breaks=seq(0,7,by=1))+
scale_colour_manual(values=c("#0072B2",  "#009E73", "#F0E442", "#D55E00"))+
xlab('Stimulus type') +
scale_x_continuous(limits=(c(0.5, 2.5)), breaks = c(0.9, 1.9), labels = levels(df\$StimulusType))+
ylab('Mean Rating') +
guides(colour = guide_legend(override.aes = list(alpha = 1))) +
geom_segment(data = summaryData
, mapping =  aes(x=xmin
, xend=xend
, y=meanRating
, yend =meanRating
, group = NA
, colour = Condition)
, lwd = 3
, show.legend = FALSE
) +
theme_bw()
`````` ## Calculating means with stat_summary for two different groupings and plotting in one plot

By : user3154843
Date : March 29 2020, 07:55 AM
may help you . I am having issues with plotting two calculated means using stat_summary in the same figure. , You can just add more layers, defining the aes for each seperately:
code :
``````ggplot(mtcars) +
stat_summary(aes(x=gear, y=hp, color=paste('cyl:', cyl), fill = paste('cyl:', cyl)), geom='ribbon', fun.data = mean_cl_normal, fun.args=list(conf.int=0.95), alpha=0.5) +
stat_summary(aes(x=gear, y=hp, color=paste('cyl:', cyl)), geom='line', fun.y = mean, size=1) +
stat_summary(aes(x=gear, y=hp, color=paste('vs:', vs), fill=paste('vs:', vs)), geom='ribbon', fun.data = mean_cl_normal, fun.args=list(conf.int=0.95), alpha=0.5) +
stat_summary(aes(x=gear, y=hp, color=paste('vs:', vs)), geom='line', fun.y = mean, size=1)
`````` 