R语言 在Plotly中使用圆环图制作面

ykejflvf  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(120)

我正在使用R和Plotly。我面临着一个与使用这个库制作小平面图有关的问题。下面你可以看到我的数据示例以及我尝试在Plotly中使用两个甜甜圈图制作小平面图的尝试。

library(plotly)
library(dplyr)
# Get Manufacturer
mtcars$manuf <- sapply(strsplit(rownames(mtcars), " "), "[[", 1)
df <- mtcars
df <- df %>% group_by(manuf)
df <- df %>% summarize(count = n())

# Donut 1
fig <- df %>%
            plot_ly(labels = ~manuf, values = ~count) %>%
            add_pie(hole = 0.6) %>%
            layout(
              title = "Donut charts using Plotly 1",
              showlegend = TRUE,
              xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
              yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
              annotations = list(
                x = 0, y = -0.1,
                showarrow = FALSE,
                xref = 'paper',
                yref = 'paper',
                align = 'left'
              ))

# Donut 2
fig1 <- df %>%
            plot_ly(labels = ~manuf, values = ~count) %>%
            add_pie(hole = 0.6) %>%
            layout(
              title = "Donut charts using Plotly 2",
              showlegend = TRUE,
              xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
              yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
              annotations = list(
                x = 0, y = -0.1,
                showarrow = FALSE,
                xref = 'paper',
                yref = 'paper',
                align = 'left'
              ))
# Making Facet
facet_fig <- subplot(
  fig,
  fig1,
  nrows = 1,
  margin = 0.05
)

facet_fig

字符串
这段代码产生两个圆环图,但问题是,这些图可以在同一个地方用命令subplot,如下面的图片所示。


的数据
谁能帮我解决这个问题,把两个甜甜圈图在同一个图(面图)?

kxxlusnw

kxxlusnw1#

我认为问题在于你如何绘制数据以及绘制什么数据:下面是一个ammanuf计数的例子:

library(plotly)
library(dplyr)

# data
mtcars$manuf <- sapply(strsplit(rownames(mtcars), " "), "[[", 1)
df <- mtcars %>% 
      group_by(am, manuf) %>% 
      summarize(count = n())

# Donut 1
fig1 <- plot_ly(df %>% 
                  count(am, manuf) %>% 
                  filter(am == 0), labels = ~manuf, values = ~n, type = 'pie', hole = 0.6,
                domain = list(x = c(0, 0.48), y = c(0, 1))) %>%
        layout(
          title = "Donut Chart 1 and 2",
          showlegend = FALSE,
          xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
          yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
        )

# Donut 2
fig2 <- plot_ly(df %>% 
                  count(am, manuf) %>% 
                  filter(am == 1),
                  labels = ~manuf, values = ~n, type = 'pie', hole = 0.6,
                domain = list(x = c(0.52, 1), y = c(0, 1))) %>%
        layout(
          title = "Donut Chart 1 and 2",
          showlegend = TRUE,
          xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
          yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
        )

facet_fig <- subplot(fig1, fig2, nrows = 1, shareX = TRUE, shareY = TRUE, titleX = FALSE, titleY = FALSE)
facet_fig

字符串


的数据

相关问题