這是一段可重現的代碼,它舉例說明了我的問題。
簡而言之,ggplot 正在繪制列變數中的實體數而不是每個變數。我想要的是一個條形圖,顯示 Lima、Sucre 和 Santiago 各自出現的頻率。
library(ggplot2)
library(dplyr)
Community <c("LIMA","LIMA","SUCRE","SUCRE","SUCRE","SANTIAGO","SANTIAGO","SANTIAGO","SANTIAGO")
df<-data.frame(Community)
df<-rename(df,'a02. Name of Community'=Community)
view(df)
ggplot(data=df, aes('a02. Name of Community'))
geom_bar()
這是我實際問題中的代碼-
將excel資料作為輸入處理
outcome<-reactive({
req(input$file1)
inFile <- input$file1
dat6<-read_excel(inFile$datapath, sheet =1)
return(dat6)
使用 tidyverse 中的 ggplot 繪圖
output$plot <- renderPlot({
Villages <- table(outcome()$'a02. Name of Community')
ggplot(data=as.data.frame(Villages), aes('a02. Name of Community'))
geom_bar()
ggplot 生成的是唯一計數的條形圖,而不是每個變數出現的次數。
如果我使用 plot() 繪制繪圖,那么它就可以作業,并且我會得到一個包含變數及其計數的條形圖。
有效的 plot() 示例如下。
Villages <- table(outcome()$'a02. Name of Community')
barplot(Villages, main='Participating Communities', horiz=TRUE, cex.names=0.8, las=1,xlab='Count of Evaluations')
uj5u.com熱心網友回復:
看起來當您嘗試重命名示例中的列時,您更改了所有變數。
如果你這樣做,它作業正常:
library(ggplot2)
library(dplyr)
Community<- c("LIMA","LIMA","SUCRE","SUCRE","SUCRE","SANTIAGO","SANTIAGO","SANTIAGO","SANTIAGO")
df<-data.frame(Communitycol=Community)
ggplot(data=df, aes(Communitycol)) geom_bar()
uj5u.com熱心網友回復:
我不知道我是否正確理解您的問題,但我會使用該as.factor功能
rm(list = ls())
library(ggplot2)
library(dplyr)
Community <-c("LIMA","LIMA","SUCRE","SUCRE","SUCRE","SANTIAGO","SANTIAGO","SANTIAGO","SANTIAGO")
df<-data.frame(Community)
df<-rename(df,'a02. Name of Community'=Community)
df$Community<-as.factor("LIMA","SUCRE","SANTIAGO")
ggplot(data=df, aes(x=Community))
geom_bar(aes(fill=Community)) theme_bw()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371956.html
