简体   繁体   中英

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: 在此处输入图像描述 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? If I just map the country (location) on color in the bar chart the resulting colors do not correspond. 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? (as you can see from the image, they are kind of shifted one with respect to the other)

Try adding layout(barmode = "overlay")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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