R donut plot

library(tidyverse)
df<- data.frame(variable=rep(c("亲朋好友", "医生", "媒体网络", "医学书籍", "其它")),
                 value=c(3137,2276,2056,84,7))
df$prop <- round(df$value/sum(df$value),2)
df$variable <- factor(df$variable, levels = df$variable[order(df$prop)])

p <- ggplot(df, aes(x = "", y = value, fill = variable)) +
  geom_bar(width = 1, stat="identity") +
  coord_polar("y", start=pi / 3) + ggtitle("Pie Chart")

p <- ggplot(df, aes(x = variable, y = prop, fill = variable)) +
  geom_bar(stat="identity")+
  coord_polar("y")+
  theme_bw()
print(p)

p <- ggplot(df, aes(x = variable, y = value, fill = variable)) +
  geom_bar(width = 0.9, stat="identity") +
  coord_polar("y")+
  theme_bw()
print(p)

p <- ggplot(df, aes(x = variable, y = value, fill = variable)) +
  geom_bar(width = 1, stat="identity") +
    coord_polar(theta = "x",direction=-1)
print(p)


p <- ggplot(df, aes(x = variable, y = value, fill = variable)) +
  geom_bar(width = 1, stat="identity") +
  coord_polar(theta = "y",direction=1)
print(p)

#https://rpubs.com/cardiomoon/398623
devtools::install_github("cardiomoon/moonBook")
devtools::install_github("cardiomoon/webr")
require(ggplot2)
require(moonBook)
require(webr)

data(acs,package = "moonBook")
PieDonut(acs,aes(pies=Dx,donuts=smoking))
PieDonut(acs,aes(Dx,smoking),ratioByGroup=FALSE)

PieDonut(acs,aes(Dx,smoking),selected=1,labelposition=0,title="labelposition=0")
PieDonut(acs,aes(Dx,smoking),selected=1,labelposition=1,title="labelposition=1")
PieDonut(acs,aes(Dx,smoking),explode=1)
PieDonut(acs,aes(Dx,smoking),explode=1,explodeDonut=TRUE)
PieDonut(acs,aes(Dx,smoking),explode=1,selected=c(3,6,9),explodeDonut=TRUE)
PieDonut(acs,aes(Dx,smoking),start=3*pi/2,explode=1,selected=c(3,6,9),explodeDonut=TRUE)
PieDonut(acs,aes(Dx,smoking),start=3*pi/2,explode=1,selected=c(3,6,9),explodeDonut=TRUE,title="Distribution of Smoking Status by Diagnosis")
PieDonut(acs,aes(Dx,smoking),r0=0,showPieName=FALSE)
PieDonut(acs,aes(Dx,smoking),r0=0.2,r1=0.8,r2=1.4,explode=1,start=pi/2,explodeDonut=TRUE)

devtools::install_github("JohnCoene/echarts4r")
  devtools::install_github('JohnCoene/echarts4r.assets')
library(echarts4r)
library(echarts4r.assets)
e1 <- iris %>%
  group_by(Species) %>%
  e_charts(
    Sepal.Length,
    elementId = "chart"
  ) %>%
  e_scatter(Petal.Length, Sepal.Width) %>%
  e_datazoom(show = FALSE, y_index = 0) %>%
  e_x_axis(min = 4) %>% 
  e_highlight(
    series_name = list("setosa", "versicolor", "virginica"),
    btn = "high"
  ) %>%
  e_downplay(
    series_name = list("setosa", "versicolor", "virginica"),
    btn = "down"
  ) %>%
  e_button(
    "high", "Highlight",
    class = "btn btn-primary",
    position = "bottom"
  ) %>%
  e_button(
    "down", "Downplay",
    class = "btn btn-default",
    position = "bottom"
  ) %>%
  e_legend(FALSE) %>%
  e_title("Connect charts") %>%
  e_tooltip(
    trigger = "item",
    axisPointer = list(
      type = "cross"
    )
  ) %>%
  e_image_g(
    left = 50,
    top = 30,
    z = 999,
    style = list(
      image = "reference/figures/logo.png",
      width = 70,
      height = 75,
      opacity = .95
    )
  ) %>%
  e_text_style(fontFamily = "Raleway")


e2 <- iris %>%
  group_by(Species) %>%
  e_charts(Petal.Length) %>%
  e_line(Sepal.Length) %>%
  e_connect("chart") %>%
  e_datazoom(y_index = 0) %>%
  e_x_axis(min = 0) %>%
  e_legend(
    bottom = 5,
    icons = ea_icons(
      c("warning", "signal", "photo")
    )
  ) %>%
  e_tooltip(
    trigger = "item",
    axisPointer = list(
      type = "cross"
    )
  ) %>%
  e_text_style(fontFamily = "Raleway")

e_arrange(e1, e2, rows = 1, cols = 2)


mtcars %>%
  head() %>%
  mutate(model = row.names(.)) %>%
  e_charts(model) %>%
  e_pie(carb)

df <- data.frame(
  grp = c("A", "A", "A", "B", "B", "B"),
  labels = rep(LETTERS[1:3], 2),
  values = runif(6, 1, 5)
)

df %>%
  group_by(grp) %>%
  e_charts(labels) %>%
  e_pie(values)

评论

此博客中的热门博文

V2ray websocket(ws)+tls+nginx分流

Rstudio 使用代理