Home > Blockchain >  Plotting Bar chart in R
Plotting Bar chart in R

Time:06-05

I am new to R programming. I need to create a bar plot in R from the data shown.

df<-structure(list(GaPa_NaPa = c("Gaidahawa", "Kanchan", "Kotahimai", "Marchawari", "Mayadevi"), Total.Area..ha. = c(9657L, 5835L, 5812L, 4844L, 7228L), Forest.Area.ha. = c(726L, 1276L, 0L, 0L, 0L), Forest.Percent = c(7.52, 21.87, 0, 0, 0), Forest.Area..Fraction. = c(0.04, 0.11, 0, 0, 0), Household.No = c(8612L, 9828L, 5939L, 5305L, 8003L), Family.Size = c(10020L, 10483L, 7921L, 6972L, 10040L),Total = c(56529L, 42528L, 46417L, 41058L, 57341L)), row.names = c(NA, 5L), class = "data.frame")

I want to plot bar graph showing GaPa_NaPa to Forest. Percent as shown (created in Excel) below. enter image description here

How can we code this using ggplot to plot the barplot above.

CodePudding user response:

Update: How to center title:

library(ggplot2)

ggplot(data = RupandehiForest2014,aes(x=GaPa_NaPa, y = Forest.Percent)) 
  geom_col(fill = "greenyellow") 
  theme_minimal() 
  labs(title = "Forest Percent", x="GaPa_NaPa", y="Forest.Percent")   
  guides(fill ="none")  
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1)) 
  theme(plot.title = element_text(hjust = 0.5, size=20))

enter image description here First answer: Here is one way how we could do it!

library(ggplot2)

ggplot(data = RupandehiForest2014,aes(x=GaPa_NaPa, y = Forest.Percent)) 
  geom_col(fill = "greenyellow") 
  theme_minimal() 
  labs(title = "Forest Percent", x="GaPa_NaPa", y="Forest.Percent")   
  guides(fill ="none")  
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))

data:

structure(list(GaPa_NaPa = c("Butwal", "Devdaha", "Gaidahawa", 
"Kanchan", "Kotahimai", "Lumbini Sanskritik", "Marchawari", "Mayadevi", 
"Omsatiya", "Rohini", "Sainamaina", "Sammarimai", "Siddharthanagar", 
"Siyari", "Sudhdhodhan", "Tillotama"), STATE_CODE = c(5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), DISTRICT = c("RUPANDEHI", 
"RUPANDEHI", "RUPANDEHI", "RUPANDEHI", "RUPANDEHI", "RUPANDEHI", 
"RUPANDEHI", "RUPANDEHI", "RUPANDEHI", "RUPANDEHI", "RUPANDEHI", 
"RUPANDEHI", "RUPANDEHI", "RUPANDEHI", "RUPANDEHI", "RUPANDEHI"
), Type_GN = c("Upamahanagarpalika", "Nagarpalika", "Gaunpalika", 
"Gaunpalika", "Gaunpalika", "Nagarpalika", "Gaunpalika", "Gaunpalika", 
"Gaunpalika", "Gaunpalika", "Nagarpalika", "Gaunpalika", "Nagarpalika", 
"Gaunpalika", "Gaunpalika", "Nagarpalika"), Province = c(5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), 
    Total.Area..ha. = c(10139L, 13667L, 9657L, 5835L, 5812L, 
    11194L, 4844L, 7228L, 4844L, 6449L, 16082L, 5066L, 3595L, 
    6620L, 5743L, 12592L), Forest.Area.ha. = c(4958L, 7352L, 
    726L, 1276L, 0L, 0L, 0L, 0L, 1L, 17L, 9115L, 0L, 11L, 142L, 
    20L, 1358L), Forest.Percent = c(48.9, 53.79, 7.52, 21.87, 
    0, 0, 0, 0, 0.02, 0.26, 56.68, 0, 0.31, 2.15, 0.35, 10.78
    )), class = "data.frame", row.names = c("1", "2", "3", "4", 
"5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16"))

enter image description here

  • Related