Jessie Kember
Jessie Kember

Reputation: 21

Using ggplot2 to graph a set of data

New addition to my old post:

I apologize if it seemed as though I was expecting anyone to do the work for me! That definitely was not my intention.

using dput, the output gave me the following:

    structure(list(Reported.Behavior = structure(c(3L, 6L, 2L, 1L, 
8L, 7L, 4L, 5L), .Label = c("Alcohol-marijuana", "Depression/Suicidal Ideation", 
"Homophobic Teasing", "Parent Communication", "Parent Support", 
"Peer Victimization", "Racism", "School climate"), class = "factor"), 
    Heterosexual.Mean = c(0.2, 0.45, 0.63, 0.8, 1.79, 0.61, 1.89, 
    3.31), Heterosexual.SD = c(0.66, 0.75, 0.67, 0.97, 0.49, 
    0.67, 0.95, 0.65), Questioning.Mean = c(0.84, 0.95, 1.07, 
    1.36, 1.63, 1.03, 1.79, 2.83), Questioning.SD = c(1.33, 1.18, 
    0.95, 1.51, 0.65, 0.82, 1.13, 0.93), LGB.Mean = c(0.57, 0.56, 
    0.77, 1, 1.72, 0.82, 1.84, 3.14), LGB.SD = c(1.13, 0.9, 0.82, 
    1.16, 0.56, 0.76, 1.07, 0.8), ANOVA.F.Value = c(375.94, 166.54, 
    176.54, 138.82, 49.13, 193.31, 5.63, 231.73), ANOVA.Effect.Size = c(0.05, 
    0.03, 0.03, 0.02, 0.01, 0.03, 0, 0.03)), .Names = c("Reported.Behavior", 
"Heterosexual.Mean", "Heterosexual.SD", "Questioning.Mean", "Questioning.SD", 
"LGB.Mean", "LGB.SD", "ANOVA.F.Value", "ANOVA.Effect.Size"), class = "data.frame", row.names = c(NA, 
-8L))

I have tried various ggplot commands, such as the following: (my data set name = lgbtq)

ggplot(All.Means, aes(Mean.Values, Homophobic.Teasing, color = Mean.Values)) + geom_bar()

However, this only produces a graph for the Homophobic Teasing Means. I am trying to find a way to put all of the behavior means on the same graph (i.e., side-by-side bars, color-coded by sexuality)

I have tried to manipulate the data by producing csv files that only contain one behavior. For example:

ggplot(data = Peer.Victimization.Means, aes(x = Mean.Values, y = Peer.Victimization, color = Mean.Values)) + geom_bar(color = "black", fill = "red")

Which works, however, I would like to find a way to utilize the entire data set, as is.

I have seen posts referring to the 'melt' function, but haven't been successful with this yet. :/ Any suggestions would be greatly appreciated!

This is my first time using "R" so please know that I am very much a beginner. For a course assignment I am using a data set that has the following column titles:

Reported Behavior (includes 8 different behavior names) Heterosexual Mean (includes a value for each behavior) Questioning Mean (includes a value for each behavior) LGB Mean (includes a value for each behavior)

I would like to use ggplot2 to graph this data. It would be ideal if I could produce a bar graph that has the following:

Y Axis: "Mean Value" X Axis: "Reported Behavior", for each reported behavior, I would like to have 3 separate bars, side by side (Heterosexual Mean value, Questioning Mean value, and LGB Mean value). Then, it would be ideal if I could color code these.

So, overall, the Y axis represents the Mean values, and the X axis lists all reported behaviors, each with 3 bars comparing the 3 different sexualities. Any help would be GREATLY appreciated!!!!

Jessie

Upvotes: 2

Views: 419

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226936

Rearrange data:

library(reshape2)
mdat <- melt(dat[,1:7]) ## Drop ANOVA vars
mdat <- data.frame(mdat,colsplit(mdat$variable,"\\.",c("type","val")))
cdat <- dcast(mdat,Reported.Behavior+type~val)

Draw the picture.

library(ggplot2)
ggplot(cdat,aes(x=Reported.Behavior,y=Mean,fill=type))+
    geom_bar(stat="identity",position="dodge")+
    ## ugly!
    ## geom_linerange(aes(ymin=Mean-SD,ymax=Mean+SD),
    ##   position=position_dodge(width=0.9))+
    coord_flip()
  • coord_flip() is nice because it makes it easier to read the labels.
  • There is commented-out code here to add lines showing +/-1 SD -- I didn't add them because the SD is so large that the plot is quite ugly -- you should think about that ...
  • bar plots are familiar, but geom_point() might be prettier (less "non-data-ink" sensu Tufte)

enter image description here

Upvotes: 2

Related Questions