使用r

发布于 2025-02-09 14:49:07 字数 1839 浏览 1 评论 0原文

我想根据以下属性表创建一个PIECHART(通过GAPA_NAPA显示Forest.area.ha。)

数据框架的数据帧如下所示: -

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")

我使用的代码是: -

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))

但是我无法进一步努力显示如何显示Piechart显示forest.area.ha。由gapa_napa撰写的代码来绘制piechart。请帮助。必须如何编写代码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渡你暖光 2025-02-16 14:49:07

这是ggplot2的另一个选项。如果您不想要标签,则可以删除geom_text_repel行。

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

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")

​“ nofollow noreferrer”> “在此处输入映像说明”

或带有值:

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")

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文