1

I have a following dataframe:

      Alue  SDP   PS  KOK KESK VIHR  VAS RKP  KD SIN PIR STL KP  FP LIB SKP EOP  IP SKE KTP Muut Vaalipiiri
1 Helsinki 13.5 12.3 21.8  2.9 23.5 11.1 5.3 1.9 0.5 1.5 0.3  0 0.9 0.3 0.2 0.2 0.1 0.1   0  3.5   Helsinki

I would like to draw a bar plot showing the 1st row value of each column on its own bar. The problem seems to be that i can't draw bar plot by row, only by column. I have tried to draw a plot like this:

ggplot(testi2, aes(testi2[0,], testi2[1,))+
  geom_col()

but it doesn't work :(

  • You may need to `gather`i.e. `testi2 %>% gather(key, val, -Alue, -Vaalipiiri) %>% ggplot(., aes(x = Alue, y = val, fill = key)) = geom_col()` – akrun Sep 13 '19 at 20:16
  • ggplot pretty much always uses long-shaped data, so you'll need to reshape this first. I'd recommend taking a look through the ggplot docs and some tutorials to see how ggplots are usually setup. One good starting point is [here](https://r4ds.had.co.nz/data-visualisation.html) – camille Sep 13 '19 at 20:52

1 Answers1

1

You need to transform your data from a wide format to a long format. There are two new functions in tidyr to do it. So install the library again.

Also, read This explanation

Data <- data.frame(
  Alue = "Helsinki",
  SDP = 13.5,
  PS = 12.3,
  KOK = 21.8
  ) #This a reduced example of your data.frame

library(tidyr)
library(ggplot2)

 Datalong <- pivot_longer(Data, cols = SDP:KOK, names_to = "name")

 ggplot(data = Datalong) + geom_col(aes(x = name, y = value))
shs
  • 3,683
  • 1
  • 6
  • 34
Orlando Sabogal
  • 1,470
  • 7
  • 20