0

I want to plot Voters_2009 and Voters_2014 both in my histogram so that I can compare both of them. My Data is

S.No PC_Name Voters_2009 Voters_2014 1 Bastar 564742 769913 2 Bilaspur 770089 1090457 3 Durg 905900 1258342 4 Janjgir-Champa 737608 1073347 5 Kanker 742076 1016943 6 Korba 745617 1052720 7 Mahasamund 776337 1131209 8 Raigarh 935750 1246186 9 Raipur 741969 1250845 10 Rajnandgaon 830578 1178296 11 Sarguja 805197 1187321

DATA<-read.csv("CHATTISGARH FOR GEOJSON.csv")
ggplot(DATA,aes(x=PC_Name,y=Voters_2009))+geom_histogram(stat="identity")

I am only able to plot Voters_2009 or Voters 2014. How do plot them simultaneously?

Daman deep
  • 631
  • 3
  • 14

2 Answers2

1

The standard trick is to reshape the data from wide to long format first, then plot grouping by a variable aesthetics.

library(ggplot2)

DATA_long <- reshape2::melt(DATA, id.vars = c("PC_Name", "S.No"))

ggplot(DATA_long, aes(PC_Name, value, fill = variable)) +
  geom_bar(stat = "identity",
           position = position_dodge()) +
  theme(axis.text.x = element_text(angle = 60, hjust = 1))

enter image description here

Edit.

If the graph is too full of bars, it may become unreadable. Facetting is a way of solving this problem. When the graph is divided into facets, the fill argument no longer tells which year is which.

ggplot(DATA_long, aes(PC_Name, value, fill = variable)) +
  geom_bar(stat = "identity",
           position = position_dodge()) +
  theme(axis.text.x = element_text(angle = 60, hjust = 1)) +
  facet_wrap(~ variable)

Data in dput format.

DATA <-
structure(list(S.No = 1:11, PC_Name = structure(1:11, .Label = c("Bastar", 
"Bilaspur", "Durg", "Janjgir-Champa", "Kanker", "Korba", "Mahasamund", 
"Raigarh", "Raipur", "Rajnandgaon", "Sarguja"), class = "factor"), 
    Voters_2009 = c(564742L, 770089L, 905900L, 737608L, 742076L, 
    745617L, 776337L, 935750L, 741969L, 830578L, 805197L), Voters_2014 = c(769913L, 
    1090457L, 1258342L, 1073347L, 1016943L, 1052720L, 1131209L, 
    1246186L, 1250845L, 1178296L, 1187321L)), class = "data.frame", row.names = c(NA, 
-11L))
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • How do I change my whole data? – Daman deep Jul 03 '19 at 10:35
  • `{library(ggplot) DATA_long <- reshape2::melt(DATA2, id.vars = c("PC_Name", "S.No")) ggplot(DATA_long, aes(PC_Name, value, fill = variable)) + geom_bar(stat = "identity", position = position_dodge()) + theme(axis.text.x = element_text(angle = 60, hjust = 1))` – Daman deep Jul 03 '19 at 10:58
  • I changed my data frame but it returns me the same graph why? – Daman deep Jul 03 '19 at 10:59
  • It ran. But I have a new problem. There is a big bar I am not able to find out why http://prntscr.com/o9zcmo. I have now computed graph for my whole data. – Daman deep Jul 03 '19 at 11:06
  • Try to see which value is greater than, say, 2500000. And given the graph, maybe it's better to facet by years, see the edit. – Rui Barradas Jul 03 '19 at 11:20
0

Instead of

ggplot(DATA,aes(x=PC_Name,y=Voters_2009))+geom_histogram(stat="identity")

try

ggplot(DATA,aes(x=PC_Name))+
     geom_histogram(aes(y=Voters_2009, color = 'red'), stat="identity") +
     geom_histogram(aes(y=Voters_2014, color = 'blue'), stat="identity")
Harsha
  • 99
  • 1
  • 6