logo
down
shadow

Plotting the means in ggplot, without using stat_summary()


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) %>% 
  summarize(grade=mean(grade)) %>% 
  ggplot(aes(group, grade)) + 
    geom_point()


Share : facebook icon twitter icon
plotting the means with confidence intervals with ggplot

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

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

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

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

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)
Related Posts Related Posts :
  • Authentication failure with rdrop2
  • DT data table display error
  • Issue when adding new rows (with nested dataframes within) to a dataframe
  • R-How to compare two dataframe and update list column value
  • Series vector for approximating pi
  • what is difference between "variance explained " in Random Forest and "merror" in XGBoost
  • R - Cast dataframe on unique rows - reshape2
  • ggplot2: plot correct proportions using geom_bar
  • Speedup query for R data.table - can this two-argument function be applied by group more quickly?
  • apply a function to several columns at once with mutate
  • R 'cowplot' neatly produce gridded plot with shared (common) legends and unique legends
  • Repeat R script for many times and save results to text file
  • How to negative lookbehind for special characters
  • data.table inner join produces error when no match is found
  • Create a new column base on existing column, but row above
  • Is there a way to visualize the process of source() in RStudio?
  • google places api consumes 10 request but I am doing only 1
  • Statistical mode of a categorical variable in R (using mlv)
  • Using for-loop to mutate a data.frame in r
  • Make plot with regression line for mixed model
  • Shortcut to select matces cases in R studio
  • vectoriced norm/matrix multiplication
  • Negative log10 transformation in R
  • Plot data with duplicate points
  • Visualizing crosstab tables with a plot in R - changing colours
  • How to manually modify automated numbers and labels in plot
  • How can I follow any redirections of a url in R?
  • Add jitter to box plot using markers in plotly
  • Adding an extra item to the legend
  • ggplot fills in data in the wrong order
  • Convert list to data frame
  • R: filtering by list(s) of strings and returning all results that start with the content of the lists
  • R:How to attach parts of a data frame with different headers and/or an overflowing piece of the dat frame
  • How to use 'par' for manipulating plot margins?
  • Can dplyr::case_when return mix of NAs and non-NAs?
  • Text preprocessing and topic modelling using text2vec package
  • Uploading multiple files in Shiny, process the files, rbind the results and return a download
  • R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable
  • Why [i] doesn't point to the starting point in a vector
  • In R after generating a mvrnorm distribution, Y, what does Y[,1] do?
  • expand a data frame to have as many rows as range of two columns in original row
  • Getting started with R and CFA
  • Re order x-axis in ggplot so time goes from 12AM to 11PM in R
  • R - Automatically stack every nth column of a data frame and save them as new objects
  • How to format dplyr output in R into doubles (or other workable format)?
  • Dataframe to matrix conversion using tapply turns zeros to NAs
  • Smallest multiple of 1:20 - How can I make it quicker?
  • How to specify the size of a graph in ggplot2 independent of axis labels
  • How can I find the number of a vector's elements in another vector?
  • ROC curve from train/test set in caret R package
  • Random Forest for a mixture of categorical,numeric and "unwanted" variables which include missing values
  • extract certain data from multiple excel files with R
  • Matrix with counts of wins and losses between methods in R
  • Grouping string variables from a dataframe by best string match to make subsets
  • Reorder does not work after adding second geom_points
  • cover POS data formate to the one can apply Arules (Apriori)
  • Matching values between data frames based on overlapping dates
  • Grouped bar chart turns into stacked bar chart ggplot
  • R: How to fill in NA Values within a Column based on grouping?
  • Two action buttons, but only the first one, that is written in the server file, works?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org