# Need help creating a barplot with data

Hi,

I'm very new to R. I took a webinar for the basics, however I didn't learn too much about creating graphs with existing data.

Now I'm trying to use R to create a barplot with some existing data that was already statistically analysed.
However, I can't find the right material that could help me with my problems, so I'm trying here:

I have following data set (sorry, I had to copy it from Excel):

|sex = female| female | female | female | male | male | male | male |
|time = LPA 1 | LPA 1 | LPA 2 | LPA 2 | LPA 1 | LPA 1 | LPA 2 | LPA 2 |
|treatm. = Contr. | PO | Contr. | PO | Contr. | PO | Contr. | PO |
|ASC = -24.05 | -8.803 | -20.615 | 0.7043 | -30.726 | -3.024 | -19.712 | -10.586 |

And I want to create a barplot that looks like this (from Excel):

With the y-axis representing the values for ASC.

I don't want to use Excel for this, as I heard it's better to use R.

Could somebody help me?
I would be already thankful for a tip what I have to look up, because I couldn't find the right stuff to help me so far.

The main problem is that the data need to be rotated to be plotted easily with ggplot. Most of the code below deals with that.

``````library(ggplot2)

#transpose the columns 2 - 9
DF2 <- as.data.frame(t(DF[,2:9]))

#name the columns using the original column 1
colnames(DF2) <- DF[,1]

#make ASC numeric
DF2\$ASC <- as.numeric(DF2\$ASC)
DF2
#>       sex  time treatm.      ASC
#> V2 female LPA_1  Contr. -24.0500
#> V3 female LPA_1      PO  -8.8030
#> V4 female LPA_2  Contr. -20.6150
#> V5 female LPA_2      PO   0.7043
#> V6   male LPA_1  Contr. -30.7260
#> V7   male LPA_1      PO  -3.0240
#> V8   male LPA_2  Contr. -19.7120
#> V9   male LPA_2      PO -10.5860

ggplot(DF2, aes(x = sex, y = ASC, fill = interaction(time, treatm.))) +
geom_col(position = "dodge") + labs(fill = "")
``````

Created on 2021-04-08 by the reprex package (v0.3.0)

Thank you so much, now it works!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.