【问题标题】:Create a slightly overlapping bar chart with plotly用 plotly 创建一个稍微重叠的条形图
【发布时间】:2021-10-23 15:09:15
【问题描述】:

我有下面的plotly 条形图,它显示已发送和点击的电子邮件。由于点击的电子邮件包含在发送中,我希望将它们显示为略微重叠而不是分组条形图。 类似:

Emails <- c("g", "o", "m")
Sent <- c(20, 24, 33)
Clicked <- c(12, 18, 29)
data <- data.frame(Emails, Sent, Clicked)
library(plotly)
fig <- plot_ly(data, x = ~Emails, y = ~Sent, type = 'bar', name = 'Sent',marker=list(color="#556361"),
               hoverinfo = paste(Emails,Sent))
fig <- fig %>% add_trace(y = ~Clicked, name = 'Clicked',marker=list(color="#A72608"),
                         hoverinfo = paste(Emails,Clicked))

fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'stack')

fig

【问题讨论】:

    标签: r plotly ggplotly


    【解决方案1】:

    好吧,实际上我不知道如何通过 plotly 语法创建所需的绘图输出。感谢plotly::ggplotly()

    这是我的基于 ggplot 的代码和(相对)情节解决方案;

    Emails <- c("g", "o", "m")
    Sent <- c(20, 24, 33)
    Clicked <- c(12, 18, 29)
    data <- data.frame(Emails, Sent, Clicked)
    
    
    data %>%
    melt(value.name = 'n',id.vars = 'Emails',variable.name = 'type')  %>%
    ggplot(aes(x=Emails,y=n,fill=type))+
    geom_col(position = position_jitterdodge(dodge.width = 0.5,jitter.height = 0,jitter.width = 0,seed = 25)) -> gg_output
    
    
    plotly_object <- plotly::ggplotly(gg_output)
    
    plotly_object
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多