1
$\begingroup$

I have a basic ggplot2 R question. I have a dataframe in which data looks like this:

AA 4
AA 6
BB 6
AB 5
BA 4
AA 3
NN 2
AN 6
NN 5
AN 4
NA 3
BB 6
BN 5
NB 1
BN 7

The file is of multiple lines. I want to make a histogram using gplot2 that will plot the 9 kinds of variables AA,BB,AB,BA,AN,BN,NN,NB,NA on the x-axis and the sum of it's corresponding values in the next column on the y-axis.

So for example, here the y-values should be

AA= 13
BB= 12
AB= 5
BA= 4
NN= 7
AN= 10
NA= 3
BN= 12
NB= 1
$\endgroup$
  • 5
    $\begingroup$ I'm voting to close this question as off-topic because it is not about bioinformatics $\endgroup$ – llrs Jan 23 '18 at 16:05
  • $\begingroup$ Sorry for the inconvenience. Should I delete the question? $\endgroup$ – rishi Jan 23 '18 at 16:23
  • $\begingroup$ No, it might help other people. But please search the internet before posting questions. (Also have a look at StackOverflow, many times it has been already asked and answered). Finding solutions to own's problem is a good skill to have. $\endgroup$ – llrs Jan 23 '18 at 16:24
2
$\begingroup$

This isn't a bioinformatics question, but it's quicker to answer than to close.

df1 <- read.table(text = "
AA 4
AA 6
BB 6
AB 5
BA 4
AA 3
NN 2
AN 6
NN 5
AN 4
NA 3
BB 6
BN 5
NB 1
BN 7", na.strings = "")

library(ggplot2)

ggplot(df1, aes(x = V1, y = V2)) +
  geom_bar(stat = "identity")

enter image description here

$\endgroup$
  • 1
    $\begingroup$ You can use geom_col() instead of geom_bar(stat = 'identity'). $\endgroup$ – Konrad Rudolph Jan 23 '18 at 14:03
  • $\begingroup$ @KonradRudolph Good to know, I don't think that function was around when I started with ggplot2. $\endgroup$ – Devon Ryan Jan 23 '18 at 14:05
  • $\begingroup$ Yes, I think it was added somewhat recently (precisely because geom_bar has such a wonky default). $\endgroup$ – Konrad Rudolph Jan 23 '18 at 14:05
  • $\begingroup$ "This isn't a bioinformatics question, but it's quicker to answer than to close." Nevertheless we can close it. $\endgroup$ – llrs Jan 23 '18 at 16:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.