0

I want a chart like this

enter image description here
I plot a pie chart in dashboard, but I want to plot a pie chart for the selected item in combobox, with the function

my Data

State=c ('USA', 'Belgium', 'France','Russia')
totalcases= c(553, 226, 742,370)
totalrecovered=c(12,22,78,21)
totaldeath=c(48,24,12,22)
DTF = data.frame(State,totalcases,totalrecovered,totaldeath)

My code to plot one :

labels=c("unrecovered","death","recovered")
USA=filter(DTF,DTF$State=="USA" )
USA=c(USA$Totalcases,USA$Totaldeath,USA$Totalrecovred)
        p1= plot_ly(labels = ~labels,
                 values = ~USA, type = 'pie',
                 marker = list(colors = brewer.pal(7,"Spectral")))
        p1

Thanks.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Eden
  • 23
  • 7
  • Please take a look at [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), to modify your question, with a smaller sample taken from your data (check `?dput()`). Posting images of your data or no data makes it difficult to impossible for us to help you! – massisenergy Apr 04 '20 at 14:35
  • i edit my question, thank you for advice :) – Eden Apr 04 '20 at 16:04

1 Answers1

1

The problem is: your dataset is a total mess.(; Try this:

library(plotly)
library(RColorBrewer)
library(dplyr)
library(tidyr)

State=c ('USA', 'Belgium', 'France','Russia')
totalcases= c(553, 226, 742,370)
totalrecovered=c(12,22,78,21)
totaldeath=c(48,24,12,22)
DTF = data.frame(State,totalcases,totalrecovered,totaldeath)

dtf_long <- DTF %>% 
  pivot_longer(-State, names_to = "labels") %>% 
  mutate(labels = gsub("total", "", labels),
         labels = ifelse(labels == "cases", "unrecovered", labels))
dtf_long
#> # A tibble: 12 x 3
#>    State   labels      value
#>    <fct>   <chr>       <dbl>
#>  1 USA     unrecovered   553
#>  2 USA     recovered      12
#>  3 USA     death          48
#>  4 Belgium unrecovered   226
#>  5 Belgium recovered      22
#>  6 Belgium death          24
#>  7 France  unrecovered   742
#>  8 France  recovered      78
#>  9 France  death          12
#> 10 Russia  unrecovered   370
#> 11 Russia  recovered      21
#> 12 Russia  death          22

usa <- filter(dtf_long, State == "USA")

p1 <- usa %>% 
  plot_ly(labels = ~labels,
          values = ~value, type = 'pie',
          marker = list(colors = brewer.pal(7, "Spectral")))
p1

Created on 2020-04-04 by the reprex package (v0.3.0)

stefan
  • 90,330
  • 6
  • 25
  • 51