logo
Tags down

shadow

Add number of observations per group in barplot (ggplot2)


By : Andrei Arkhanguelski
Date : July 31 2020, 09:00 PM
To fix the issue you can do As you have only one value to plot as a label per interval, you can filter your dataframe using distinct function from dplyr and set a single y value to plot it:
code :
library(tidyverse)
Euk_one %>% 
  drop_na(`Length Stretch`) %>%
  mutate_at(c("GGA","GGG","GGT","GGC"),~.x/`Length Stretch`) %>% 
  group_by(interval) %>% dplyr::select(c(interval, "GGA","GGG","GGT","GGC", Obs))%>%
  summarize_all(mean) %>%pivot_longer(-c(interval, Obs)) %>%
  ggplot(aes(x=interval, y=value, fill=name)) + geom_col() +
  ggtitle("Eukarya 1 Codon Stretches - Distribution of codons ")+
  labs(fill= "Codons") +
  theme(axis.text.x = element_text(angle = 45)) + 
  geom_text(inherit.aes = FALSE, data = . %>% distinct(interval, Obs), 
            aes(x = interval, y = 0.25, label=Obs))


Share : facebook icon twitter icon

How to add a number of observations per group and use group mean in ggplot2 boxplot?


By : Nomide
Date : March 29 2020, 07:55 AM
I wish this helpful for you Is this anything like what you're after? With stat_summary, as requested:
code :
# function for number of observations 
give.n <- function(x){
  return(c(y = median(x)*1.05, label = length(x))) 
  # experiment with the multiplier to find the perfect position
}

# function for mean labels
mean.n <- function(x){
  return(c(y = median(x)*0.97, label = round(mean(x),2))) 
  # experiment with the multiplier to find the perfect position
}

# plot
ggplot(mtcars, aes(factor(cyl), mpg, label=rownames(mtcars))) +
  geom_boxplot(fill = "grey80", colour = "#3366FF") +
  stat_summary(fun.data = give.n, geom = "text", fun.y = median) +
  stat_summary(fun.data = mean.n, geom = "text", fun.y = mean, colour = "red")

Add number of observations per group in ggplot2 boxplot


By : user3271035
Date : March 29 2020, 07:55 AM
Does that help Following this question: How to add a number of observations per group and use group mean in ggplot2 boxplot?, I want to add number of observations per group in ggplot boxplot too. But I have added a colour into aes mapping. , You can just use position:
code :
p <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) +  
     geom_boxplot() +
     stat_summary(fun.data = give.n, geom = "text", fun.y = median,
                  position = position_dodge(width = 0.75))
p
p2 <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(cyl))) + 
      geom_boxplot() +
      stat_summary(fun.data = give.n, geom = "text", fun.y = median, 
                   position = position_dodge(width = 0.75))
p2

Add a number of observations per group AND SUBGROUP in ggplot2 boxplot


By : Swatantra Soni
Date : March 29 2020, 07:55 AM
Any of those help This might seem like a duplicate of this question, but in fact I want to expand the original question. , is it what you want?
code :
require(ggplot2)

give.n <- function(x){
  return(c(y = median(x)*1.05, label = length(x))) 
  # experiment with the multiplier to find the perfect position
}

ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(gear))) +
  geom_boxplot() +
  stat_summary(fun.data = give.n, geom = "text", fun.y = median, position=position_dodge(width=0.75))

Add group mean line to barplot with ggplot2


By : yaro
Date : March 29 2020, 07:55 AM
may help you . I combined the solution from @bouncyball with my original approach using `geom_errorbar.
Here is the code:
code :
df.mean = df %>% 
  group_by(class) %>% 
  mutate(ymean = mean(y))

ggplot(df, aes(x, y, fill=class)) +
  geom_col() +
  geom_errorbar(data=df.mean, aes(x, ymax = ymean, ymin = ymean),
               size=0.5, linetype = "longdash", inherit.aes = F, width = 1)

Barplot of numerical categorical data of number of observations ordered numerically in matplotlib


By : man
Date : March 29 2020, 07:55 AM
I wish this help you I assume you want to order the bars by the value of the index. That can be accomplished by using sort_index():
code :
df['income_cat'].value_counts().sort_index().plot(kind='bar',sort_columns=True)
Related Posts Related Posts :
  • multiple usage of ggplot
  • Calculate the number of common occurrences and values ​for each id in R studio
  • Splitting full address column in multiple columns
  • Data.table - Subtract pairs of columns
  • Vector containing a numerical value not being equal to the actual value
  • Extracting all words and clusters of letters in a string and then making each word a seperate piece of data using gsub()
  • Count Duplicates In Vector In R
  • tmap coming up with blank map with one variable (but values are there)
  • Making indexing a row number in map_map2_chr function
  • find frequency of events in groups dplyr
  • Using case_when() within mutate_at() to recode rows of selected columns with different types with NA
  • R programming: How to count different values’ frequency among all columns?
  • ggplot2: geom_point is sometimes removing NA values depending on the aesthetic used
  • Trying to map a value for geom_vline, but is not plotting in the correct place on the x axis with ggplot in R
  • R - find overlapping dates per group based on another data frame
  • In R how to plot the tail area of a normal distribution using ggplot?
  • Create a loop to generate a series of dataframes in R
  • Is there an R function to select one variable from each group (group_by()) from the dataframe?
  • Alternative of summarise() function in dplyr
  • How to I get scatter plot to have different colour for each value that I am plotting?
  • R: Avoid using a for-loop to sequentially select values in one column and apply a function using the vector of values in
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org