简体   繁体   English

plotly R 子图/高亮,大小颜色问题

[英]plotly R subplot / highlight, size an color problem

I am doing a subplot with a linked barchart and line chart (code below) and I am having a few issues:我正在使用链接的条形图和折线图(下面的代码)做一个子图,我遇到了一些问题:

library(readr)
library(dplyr)
library(plotly)
library(crosstalk)
library(forecast)
library(ggplot2)
library(tidyverse)
library(readr)


data <- read.csv(url("https://covid.ourworldindata.org/data/owid-covid-data.csv"))
data$date <- as.Date(data$date)

shared_data <- SharedData$new(data, key = ~location)

col <- shared_data %>%
  plot_ly() %>%
  mutate(location=as.character(location)) %>%
  filter(date == "2020-12-07") %>%
  filter(continent == "Europe" & location != "Russia" & population > 9000000) %>%
  mutate(location = fct_reorder(location, total_cases_per_million, .desc = TRUE)) %>%
  add_bars(x = ~location, y = ~total_cases_per_million, color = I("grey")) %>%
  layout(yaxis = list(title = "Total Covid-19 cases per million people")) %>%
  hide_legend()

lines <- shared_data %>%
  plot_ly() %>%
  filter(continent == "Europe" & location != "Russia" & population > 9000000) %>%
  add_lines(x = ~date, y = ~new_cases_smoothed_per_million, color = ~location) %>%
  layout(yaxis = list(title = "New Covid-19 cases (7 days avg) per million people"))


subplot(col, lines, titleY = TRUE) %>%
  hide_legend() %>%
  highlight(on = "plotly_hover") %>%
  layout(title = "Covid-19 incidence in largest European countries")

1- highlight: when I highlight, the bars of the bar chart get sort of split in two: 1- 突出显示:当我突出显示时,条形图的条形会分成两部分: 在此处输入图像描述 Does anyone knows how to fix this?有谁知道如何解决这个问题?

2- color: how can I get to have the same color for the same country in both graphs? 2-颜色:我怎样才能在两个图表中为同一个国家/地区使用相同的颜色? If I just map the country (location) on color in the bar chart the resulting colors do not correspond.如果我只是 map 条形图中颜色上的国家(位置),则生成的 colors 不对应。 I tried a couple of lines I found by googling around, but nothing seems to work.我尝试了几行通过谷歌搜索找到的行,但似乎没有任何效果。

3- How can I get the two graphs having the same size? 3-我怎样才能得到两个大小相同的图? (as you can see from the image, they are kind of shifted one with respect to the other) (从图像中可以看出,它们相对于另一个有点偏移)

Try adding layout(barmode = "overlay")尝试添加layout(barmode = "overlay")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM