I am having trouble visualizing my data. I hope someone can help. I actually have multiple problems.
Data
crap <- tribble(~Person, ~Group, ~Measurement_1, ~Measurement_2, ~Measurement_3,
"Alex","A",14.6,52,115,
"Alex","B",12.3,63,116,
"Alex","C",15.5,45,186,
"Alex","D",16.3,35,146,
"Alex","E",18.7,66,149,
"Dante","A",19.6,57,185,
"Dante","B",15.2,75,187,
"Dante","C",12.9,64,199,
"Dante","D",11.3,69,142,
"Dante","E",13.4,43,134,
"Dante","A",18.7,44,132,
"Clyde","B",12.9,62,135,
"Clyde","C",11.5,65,165,
"Clyde","D",10.5,60,183,
"Clyde","E",11.9,55,145)
Objectives:
Reorder "Person" based on the values of Measurement_1, Measurement_2, Measurement_3
Because I want bar plots with increasing order of Y-value, i rearranged the "Person" column based on the values found in columns "Measurement_1", "Measurement_2", "Measurement_3" using
fct_reorder()
blue <- crap %>%
mutate(reorder_M1 = fct_reorder(Person, Measurement_1)) %>%
mutate(reorder_M2 = fct_reorder(Person, Measurement_1)) %>%
mutate(reorder_M3 = fct_reorder(Person, Measurement_1))
Create a bar plots based on a single data frame using the pairings of the column found below.
plot1 < x = reorder_M1, y = Measurement_1
plot2 < x = reorder_M2, y = Measurement_2
plot3 < x = reorder_M3, y = Measurement_3
However, this is where i get an error, when adding error bars using geom_errorbar()
, i get an error saying Error in Measurement_1 - sd : non-numeric argument to binary operator
which seems to not recognize sd
as standard deviation value.
Also, is there a way to create multiple barplots using the pairing above using a a single block of code. Is this possible without melting the data? Which apply
would be most appropriate for this
Thanks ,I shope someone can enlighten me.
Here is the code I am trying:
crap_plot <- ggplot(data = blue,
aes(x = reorder_M1,
y = Measurement_1,
fill = Group))
geom_bar(stat = "identity", position = "dodge")
geom_errorbar((aes(ymin=Measurement_1-sd, ymax=Measurement sd)))
CodePudding user response:
Note that using geom_errorbar like this you can fix doing geom_errorbar((aes(ymin=Measurement_1-sd(Measurement_1), ymax=Measurement sd(Measurement_1))))
but you do get on every group the same bar, it does not do it group wise.
I recommend using this instead, which will only show your errors for "Dante" in group "A" as your sample data has only one value for the other groups making SD=0.
ggplot(blue, aes(x = reorder_M1, y = Measurement_1, fill = Group))
stat_summary(fun = mean, geom = "bar", position = "dodge")
stat_summary(fun.data = "mean_se", geom = "errorbar", position = position_dodge(width = 0.90), width = 0.3)