Error_bar and exercise - r

Sorry my bad english, for not pasting the code, and for asking questions because I am not very familiar with R. I am a beginner. There's my notice and the graph I must find:
I read the R documentation to solve this problem but I was unable to figure out the solution.
Actually I found this with this script I used. But i've got not clue for adding errorbar I tried geom_errorbar(aes(ymin = mean-se, ymax = mean+se)) but surely I've mistaken myself
`rm(list=ls() )
library(dplyr)
library(ggplot2)
library(ggpubr)
Sparrows <- read.delim("C:/Users/detar/Downloads/Sparrows.txt")
View(Sparrows)
str(Sparrows)
jitter<-filter(Sparrows,day == 4)
x<-ggplot(jitter,
aes (x = rank_name,
y = logit.motility,)) + geom_point(colour = "cyan") +
xlab("Social Rank") +
ylab("Logit(Proportion of motile sperm") +
labs(title =("Ejaculate quality covaries with social rank
of male House Sparrows")) +
theme(plot.title = element_text(hjust = 0.5)) +
scale_x_discrete(breaks=c("D","S1","S2","S3"), labels=c("Dominant", "Subordinate 1", "Subordinate 2", "Subordinate 3"))
x2<-x + theme_classic() + theme(plot.title = element_text(hjust = 0.5, size = 14))
Thanks for your help
Benjamin
So I added
table <- jitter %>%
group_by(rank_name) %>%
summarize(Mean = mean(logit.motility, na.rm=TRUE),
SEM = sd( logit.motility, na.rm=TRUE) / sqrt(15)
) %>% as.data.frame()
x2<-x + theme_classic() + theme(plot.title = element_text(hjust = 0.5, size = 14)) + geom_errorbar(data = summary_table,
aes(x =rank_name,
y =logit.motility,
ymin =Mean - SEM ,
ymax =Mean + SEM ,
colour = "black",
width = 1 ))
But an error occured
Warning: Ignoring unknown aesthetics: y So am i mistaken in those arguments
And a new time, i thank you

The solution to this question is identical to the solution here but all possible duplicates I have found are about bar plots, so here is one answer to a question about scatter plots.
First, the data, since the question has none.
df1 <- iris[4:5]
Now the graph. Any of geom_errorbar or stat:summary with geom = "errorbar" could be used.
library(ggplot2)
ggplot(df1, aes(x = Species, y = Petal.Width)) +
geom_point(aes(colour = "lightblue")) +
scale_color_manual(values = "lightblue") +
stat_summary(geom = "point", fun.y = mean) +
stat_summary(geom = "errorbar", fun.data = mean_se,
position = "dodge", width = 0.2)

Related

Move position of text in ggplot

Hopefully a pretty simply problem to solve - I have produced the boxplot pictured using the following code
ggplot(minanalysis, aes(x=MinAnaType, y=Calcium)) +
geom_boxplot() +
geom_point(aes(colour=Ploidy)) +
stat_summary(fun.y=mean, colour="black", geom="point", size=2,show.legend = FALSE) +
geom_text(data = means, vjust = -0.25, hjust=-0.3, size = 3, aes(label = round(Calcium, 2),
y = Calcium + 0.08)) +
#add a sample size n =
geom_text(data = minanalysis %>% group_by(MinAnaType) %>%
summarize(Count = n(),
Calcium=max(Calcium) + 0.05 * diff(range(minanalysis$Calcium))),
aes(label = paste0("n = ", Count)),
position = position_dodge(0.85), size=3, show.legend = FALSE) +
# Add pairwise comparisons p-value and global p-value
stat_compare_means(comparisons = my_comparisons, label = "p.signif", label.y = 9500, size = 4) +
stat_compare_means(label.y = 10500, size = 4)
What I want is for the "n=290" and n=50" to be located underneath x axis labels 'Eviscerated' and 'WholeBody'. Having them above the boxplots themselves is too messy. Could somebody help with the code for this, I'm new to R studio.
Thanks
The easiest is probably to add a linebreak to your x labels. You can then add your count, either hardcoded, or programmatically.
(This would be within scale_x_...(labels = ...))
E.g.
library(tidyverse)
ggplot(filter(iris, Species %in% c('setosa','virginica')), aes(Species, Sepal.Length)) +
geom_boxplot() +
scale_x_discrete(labels = c(setosa = paste('setosa', '\nn = 30'),
virginica = paste('virginica', '\nn = 30')))

Bar plot ggplot2 - Error: Aesthetics must be either length 1 or the same as the data (150): fill, x, y

Hey I know there are lots of questions about this particular error but i still cant find what is wrong, pretty new to R and coding in general.
here is a link to may data
https://www.dropbox.com/s/qfo5rp7ywgsgxhy/CRERDATA.csv?dl=0
and here is my code to make the graph
not all used for graph obviously
library(car)
library(ggplot2)
library(Rmisc)
library(dunn.test)
library(FSA)
summarizes my data so i can get standard error bars
index_sum <- summarySE(CRERDATA, measurevar = "index", groupvars = c("site", "scenario"), na.rm = TRUE)
graph code
index_graph <- ggplot(CRERDATA, aes(x = index_sum$site, y = index_sum$index, fill = index_sum$scenario)) +
geom_bar(aes(fill = index_sum$scenario), position = position_dodge(), stat="identity") + ylab("Bleaching index") + xlab("Sites") +
labs(fill = "scenario") + scale_fill_grey() + theme_minimal() +
geom_errorbar(aes(ymin = index_sum$index-se, ymax = index_sum$index+se), width = .2, position = position_dodge(.9), color = "red")
You have specified CRERDATA in the ggplot as data, when you are actually using index_sum.
Load necessary packages:
library(ggplot2)
library(Rmisc)
Read data and summarise:
CRERDATA <- read.csv('CRERDATA.csv')
index_sum <- summarySE(CRERDATA, measurevar = "index", groupvars = c("site", "scenario"), na.rm = TRUE)
Instead of CRERDATA, use index_sum. You then don't need to use the dollar sign $ to access columns:
ggplot(index_sum, aes(x = site, y = index, fill = scenario)) +
geom_bar(aes(fill = scenario), position = position_dodge(), stat="identity") +
ylab("Bleaching index") + xlab("Sites") + labs(fill = "scenario") + scale_fill_grey() +
theme_minimal() +
geom_errorbar(aes(ymin = index-se, ymax = index+se), width = .2, position = position_dodge(.9), color = "red")
The result:

Dual "y" axis in ggplot2 plot [duplicate]

This question already has an answer here:
Dual y axis (second axis) use in ggplot2
(1 answer)
Closed 5 years ago.
I know this topic has arisen some time in different threads of this page, but I am afraid that following the instructions of all of them I have not managed to fix it. I have been trying to solve this problem for a week that seems quite trivial and I can not find the way.
I do not know if it's about differences in the graphics or that there is something I do wrong. The case is as follows. I have two graphics using the ggplot2 package:
library(ggplot2)
data<-data.frame(Age=0,var2=0,var1=0,inf=0,sup=0,ppv=0)
data[1,]<-c(1,1,0.857,0.793,0.904,0.03)
data[2,]<-c(1,2,0.771 ,0.74,0.799,0.056)
data[3,]<-c(1,3,0.763 ,0.717,0.804,0.06)
data[4,]<-c(1,4,0.724 ,0.653,0.785,0.09)
data[5,]<-c(2,1,0.906,0.866,0.934,0.055)
data[6,]<-c(2,2,0.785 ,0.754,0.813,0.067)
data[7,]<-c(2,3,0.660,0.593,0.722,0.089)
data[8,]<-c(2,4,0.544,0.425,0.658,0.123)
pd <- position_dodge(0.2) #
names(data)<-c("Age","var2","var1","inf","sup","ppv")
data$Age<-as.character(data$Age)
data$var2<-as.character(data$var2)
p<- ggplot(data, aes(x=var2, y=var1, colour=Age)) +
geom_errorbar(aes(ymin=inf, ymax=sup), width=.1 , position=pd) +
geom_line(position=pd,aes(group=Age),linetype=c("dashed")) +
geom_point(position=pd,size=3) +
theme_light()+
ylim(0,1) +
scale_color_manual(values=c("1"="grey55","2"="grey15"))+guides(fill=guide_legend(nrow=2,byrow=TRUE)
)
s<- ggplot(data, aes(x=var2, y=ppv, colour=Age)) +
geom_line(position=pd,aes(group=Age),linetype=c("dashed")) +
geom_point(position=pd,size=3) +
theme_light()+
ylim(0,0.2) + scale_color_manual(values=c("1"="grey55","2"="grey15"))+guides(fill=guide_legend(nrow=2,byrow=TRUE)
)
They look like this:
Image of p
Image of s
I was wondering if someone would know the way to put them together in a single graph, with the two scales that they currently have, for example, the y axis of the graph p at the left side and the y axis of the graph s at the right side since I can not directly draw both data in a graph due to the radical difference in the scales .
Thank you very much for your time,
Best regards,
try this code, you should set aes at new layer.
ggplot(data, aes(x = var2, y = var1, colour=Age)) +
geom_errorbar(aes(ymin = inf, ymax = sup), width = .1, position = pd) +
geom_line(position = pd, aes(group = Age), linetype = c("dashed")) +
geom_point(position = pd, size = 3) +
geom_line(position = pd, aes(x = var2, y = ppv * 5, colour = Age, group = Age), linetype = c("dashed"), data = data) +
geom_point(aes(x = var2, y = ppv * 5, colour = Age, group = Age), position = pd, size = 5) +
theme_light() +
scale_color_manual(values = c("1" = "grey55", "2" = "grey15")) +
scale_y_continuous(sec.axis = sec_axis(~./5)) +
guides(fill = guide_legend(nrow = 2, byrow = TRUE))

how to combine 2 DVs in one graph using ggplot

I am trying to combine 2 dependent variables (or 2 graphs) in one graph using ggplot function. All the suggestions I could find online were not really helpful in my case.
Graph1 <- ggplot(mydata, aes(age, conf))
Graph1 + stat_summary(fun.y = mean, geom = "point") +
stat_summary(fun.y = mean, geom = "line", aes(group = 1)) +
stat_summary(fun.data = mean_cl_boot, geom = "errorbar", width = 0.2) +
labs(x = "Age Group", y = "Accuracy (%)") + ylim(0, 1)
Graph2 <- ggplot(mydata, aes(age, acc))
Graph2 + stat_summary(fun.y = percent(1), geom = "point") +
stat_summary(fun.y = mean, geom = "line", aes(group = 1), linetype = "dashed") +
stat_summary(fun.data = mean_cl_boot, geom = "errorbar", width = 0.2) +
labs(x = "Age Group", y = "Accuracy (%)") + ylim(0, 1)
In addition to this, I will need to have the means and error bars not overlapping. Any advice would be greatly appreciated.
After further investigation I have found the following suggestion which seems to be a great solution. However, I cannot install tidyr as it incompatible with the current R version. I have tried different options to download the package, without success.
library(tidyr)
home.land.byyear <- gather(housing.byyear, value = "value", key = "type",
Home.Value, Land.Value)
ggplot(home.land.byyear, aes(x=Date, y=value, color=type)) + geom_line()
see http://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html

How can I add mean labels to a bar chart?

I would like to add the mean of each condition at the base of my bar chart in R. The final product looks something like this in excel (note the means are displayed at the base of each bar):
My current code is as follows:
pmrtBar <- ggplot(reslagdfClean,aes(x=Condition, y=PMMissProp*100)) + stat_summary(fun.y = mean, geom = "bar", fill = cbPalette) +
theme(axis.title=element_text(size=12)) +
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width=.1, size = .25) +
coord_cartesian(ylim=c(0,50)) +
labs(x = "Condition", y="Mean Mean Miss Proportion (%)") +
apatheme
pmrtBar
I am new the R environment. Any feedback on the code above is also appreciated.
It's always good to add a reproducible example to your question.
Converting my comment to an answer with the use of some example data:
# example data
dat <- data.frame(id = c("ACT","Blank","None"),
mn = c(0.3833,0.38,0.4033),
se = c(0.1,0.15,0.12))
# creating the plot
ggplot(dat, aes(x=id, y=mn, fill=id)) +
geom_bar(stat="identity", width=0.7) +
geom_errorbar(aes(ymax = mn + se, ymin = mn - se), width=0.25) +
geom_text(aes(y = 0.2, label = paste(mn*100, "%"))) +
labs(x = "\nCondition", y = "Proportion (%)\n") +
scale_y_continuous(breaks = seq(0.15, 0.55, 0.05), labels = scales::percent) +
coord_cartesian(ylim = c(0.15,0.55)) +
theme_minimal(base_size = 14) +
theme(panel.grid.major.y = element_line(linetype = 2, color = "grey80", size = 1))
which results in:

Resources