新手求助:如何用RStudio flexdashboard创建饼图?
Fixing Your Flexdashboard Pie Chart Issue
Hey there! Let's work through why your pie chart isn't running—there are a few straightforward fixes we can apply to get it working correctly. Here's what's going wrong and how to fix it:
Key Issues in Your Code
- Wrong file reading function: You're using
read.csv()to load an.xlsExcel file, butread.csv()only works with CSV files. For Excel files, you need a package likereadxl. - Column name mismatch: Your dataset has a column named
Refugees, but your code references~totalref$Total—this will throw an error because that column doesn't exist. - Missing pipe connection: Your
layout()call isn't linked to theplot_ly()output, so the layout settings won't apply, and the plot might not render properly.
Corrected Full Code
First, make sure you have all required packages installed (if you haven't already):
install.packages(c("flexdashboard", "plotly", "readxl"))
Then use this updated code in your flexdashboard:
--- title: "Refugee Dashboard" output: flexdashboard::flex_dashboard --- ```{r message=FALSE, warning=FALSE} library(flexdashboard) library(plotly) library(readxl) # Read the Excel file correctly with read_excel() totalref <- read_excel("F:/HU/ANLY 512/Dashboarding Lab/total.xls") # Create the pie chart with correct column names and pipe the layout plot_ly(totalref, labels = ~Country, # No need for totalref$ when using formula syntax values = ~Refugees, # Match the actual column name from your dataset type = 'pie') %>% layout(title = 'Persons of Concern in 9 European Countries', xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
Quick Additional Checks
- Double-check that your file path is correct (no typos, and the file exists at that location)
- Verify that your Excel file's column names are exactly
CountryandRefugees(no extra spaces or capitalization differences)
内容的提问来源于stack exchange,提问作者Louis
相关产品推荐
相关产品推荐

