0

I would like to exclude one column from my chart. In csv I use as my data there is a lot of empty cells and thus there is nameless column in my chart that is at the same highest of them all. In my opinion it looks a bit stupid so I would like to get rid of it.

Here is my chart code:

ggplot(df, aes(Coverage, fill=(Coverage)))+
  geom_bar(color="black",fill="brown3")+
  theme(text = element_text(size=15),axis.text.x = element_text(angle=90, hjust=1))+
  labs(title = "Diagram przedstawiajacy w ktorym miesiacu w kolejnych latach najwieksza liczba dziennikarzy poniosla smierc", x="Panstwo", y="Rok")

And here is how the chart looks like. First column is the one counting amount of empty cells. Image of chart

thank you very much for all the help!

Community
  • 1
  • 1
  • Also I would be very thankful if you could tell me why my column descriptions are not right under their columns but instead they are moved to right a bit –  Dec 28 '19 at 14:30
  • I did as you said but literally nothing changed :( –  Dec 28 '19 at 17:27

2 Answers2

0

You will need to remove those entries from your df. You could so something along the lines of df[!(df$Coverage %in% c("levels", "to", "exclude", "here")), ]. If that doesn't work, you may, in addition, need to use droplevels(), too.

When you rotate the text, you will also need to offset it a bit, too. You can do it in theme() using hjust or vjust (I always forget which one). Something along the lines of element_text(angle = 90, hjust = 0.5).

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • Is there no simpler way taht would allow me to start chart from second column not first? –  Dec 28 '19 at 14:59
  • @SauroxGM not with long format. If you have data in wide format, you could perhaps select only `2:ncol(x)`, alas... – Roman Luštrik Dec 28 '19 at 21:22
0

Alternatively to @Roman Luštrik's answer, you can use dplyr to filter your dataset and do the plot in the same sequence:

library(dplyr)
library(ggplot2)
df %>% filter(Coverage != "") %>%
  ggplot(df, aes(Coverage, fill=(Coverage)))+
  geom_bar(color="black",fill="brown3")+
  theme(text = element_text(size=15),axis.text.x = element_text(angle=90, hjust=0.5))+
  labs(title = "Diagram przedstawiajacy w ktorym miesiacu w kolejnych latach najwieksza liczba dziennikarzy poniosla smierc", x="Panstwo", y="Rok")

If this is not working for you, please consider to provide a reproducible example of your dataset (see: How to make a great R reproducible example)

dc37
  • 15,840
  • 4
  • 15
  • 32