Home > database >  Pie Chart using R
Pie Chart using R

Time:06-21

I want to create a piechart (showing Forest.Area.ha. by GaPa_NaPa) based on following attribute table as below:- enter image description here

The dataframe for the data is as shown:-

structure(list(GaPa_NaPa = c("Gaidahawa", "Kanchan", "Kotahimai", 
"Marchawari", "Mayadevi", "Omsatiya", "Rohini", "Sammarimai", 
"Siyari", "Sudhdhodhan", "Devdaha", "Lumbini Sanskritik", "Sainamaina", 
"Siddharthanagar", "Tillotama", "Butwal"), Total.Area..ha. = c(9657L, 
5835L, 5812L, 4844L, 7228L, 4844L, 6449L, 5066L, 6620L, 5743L, 
13667L, 11194L, 16082L, 3595L, 12592L, 10139L), Forest.Area.ha. = c(114.91, 
178.19, 31.37, 43.43, 152.87, 29.12, 63.16, 59.81, 36.4, 16.42, 
113.13, 422.87, 186.13, 167.2, 60.27, 45.3), Forest.Percent = c(6.67, 
10.35, 1.83, 2.52, 8.88, 1.69, 3.67, 3.47, 2.11, 0.95, 6.57, 
24.57, 10.81, 9.71, 3.5, 2.63), Forest.Area..Fraction. = c(0.07, 
0.1, 0.02, 0.03, 0.09, 0.02, 0.04, 0.03, 0.02, 0.01, 0.07, 0.25, 
0.11, 0.1, 0.04, 0.03), Household.No = c(8612L, 9828L, 5939L, 
5305L, 8003L, 6683L, 6349L, 5164L, 7889L, 7619L, 15624L, 10736L, 
17572L, 12329L, 30452L, 36989L), Family.Size = c(10020L, 10483L, 
7921L, 6972L, 10040L, 8218L, 8096L, 7303L, 9060L, 8717L, 17582L, 
13854L, 19657L, 16011L, 36399L, 51099L), Total = c(56529L, 42528L, 
46417L, 41058L, 57341L, 41080L, 43277L, 43300L, 45274L, 41472L, 
71806L, 88090L, 78477L, 76307L, 149657L, 195054L)), row.names = c(NA, 
16L), class = "data.frame")

The code I used is:-

setwd("C:/Users/lenovo/Desktop/AllAboutR/AssignmentDocs")
ForestArea2010<-read.csv("Forest2010.csv")
View(ForestArea2010)
pie(RupandehiLULC19$GaPa_NaPa, main="Piechart of Forest Area", las=3, col=hsv(12))

But I couldn't work further on how to show piechart showing Forest.Area.ha. by GaPa_NaPa working on code to plot piechart. Please help on it. How the code must be written?

CodePudding user response:

Here is another option with ggplot2. If you don't want the labels then the geom_text_repel line can be removed.

library(ggplot2)
library(ggrepel)

ggplot(RupandehiLULC19, aes(x = "", y = `Forest.Area.ha.`, fill = GaPa_NaPa))  
  geom_bar(width = 1, stat = "identity")  
  coord_polar("y", start = 0)  
  xlab("")  
  ylab("Piechart of Forest Area")  
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank())  
  geom_text_repel(aes(label = `Forest.Area.ha.`),
            position = position_stack(vjust = 0.5))

Output

enter image description here

Or another option using legend:

pie(RupandehiLULC19$Forest.Area.ha., labels = "", main="Piechart of Forest Area", las=3, col=palette(rainbow(16)))
legend(.85, 1.1, RupandehiLULC19$GaPa_NaPa, cex = 0.7, fill = palette(rainbow(16)), box.col = "white",bg = "white")

enter image description here

Or with the values:

pie(RupandehiLULC19$Forest.Area.ha., labels = RupandehiLULC19$Forest.Area.ha., main="Piechart of Forest Area", las=3, col=palette(rainbow(16)))
legend(.85, 1.1, RupandehiLULC19$GaPa_NaPa, cex = 0.7, fill = palette(rainbow(16)), box.col = "white",bg = "white")

enter image description here

  • Related