在 R 中绘制条形图

Plotting Bar chart in R

我是 R 编程新手。我需要根据显示的数据在 R 中创建条形图。

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

我想绘制条形图,显示 GaPa_NaPa 到 Forest。百分比 如下所示(在 Excel 中创建)。

我们如何使用 ggplot 编写代码来绘制上面的条形图。

更新:如何使标题居中:

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

第一个回答: 这是我们可以做到的一种方法!

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

数据:

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