绘制facet_wrap和scales =“free”图时出错在情节 [英] Error plotting chart with facet_wrap and scales = "free" in plotly

查看:122
本文介绍了绘制facet_wrap和scales =“free”图时出错在情节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含各种类别(方面)的时间序列,我试图使用 ggplot2 中的 facet_wrap code>并上传至 plotly



如果我设置 scales =fixed,我没有问题:图形在我的电脑上看起来很好,在 plotly 上。但是,如果我设置 scales =free,如下面的代码所示,那么我的电脑上的 ggplot2 看起来很好,但绘图版本无法正常显示(无论是在我的电脑还是网络上)。



一些示例代码;我为从2000年到2015年的16年期间的12个州创建了一些随机值,我想绘制这些值的时间序列,每个州都有自己的图表和唯一的y轴。

  library(ggplot2)
library(plotly)

set.seed(1)
states = sample(state.name,12)##创建12个状态的随机列表

##创建具有状态和年份值的随机数据
dat < - data.frame(Year = rep(2000:2015,times = 12),
State = rep(states,each = 16),
Value = rnorm((12 * 16),mean = 5, sd = 2))

##绘制每个状态的facet_wrap数据
p < - ggplot(dat,aes(Year,Value))+ geom_line()+
facet_wrap (〜State,ncol = 4,scales =free)
p
ggplotly(p)

ggplot2 命令(p)按预期显示:


但是 ggplotly(p )被扭曲:



据我所知,t这里有一个关于这个话题的前一个话题,但它没有解决:



Plotly Facets不能正确翻译



我很感激任何帮助。

plotly
子图。尝试:

$ $ p $ $ $ $ c $ dat $ id< - as.integer(dat $ state)
p< - plot_ly(dat ,x = Year,y = Value,group = State,
xaxis = paste0(x,id),marker = list(color =black))
p < - layout(
$ subplot(p,nrows = 3,margin = 0.05),
xaxis = list(title =Arizona),
xaxis2 = list(title =Connecticut),
xaxis3 = list(title =Florida),
xaxis4 = list(title =Georgia),
xaxis5 = list(title =Indiana),
xaxis6 = list(title = Maine),
xaxis7 = list(title =Nebraska),
xaxis8 = list(title =Nevada),
xaxis9 = list(title =New Hampshire) ,
xaxis10 = list(title =South Dakota),
xaxis11 = list(title =Tennessee),
xaxis12 = list(title =Texas),

yaxis = list(title =Value),
yaxis2 = list(title =),
yaxis3 = list(title =),
yaxis4 = list(title =),
yaxis5 = list(title =Value),
yaxis6 = list(title =),
yaxis7 = list(title =),
yaxis8 = list(title =),
yaxis9 = list(title =Value ),
yaxis10 = list(title =),
yaxis11 = list(title =),
yaxis12 = list(title =),showlegend = FALSE)
p


I have a time series with various categories (facets), and I am trying to create a chart using the facet_wrap feature from ggplot2 and upload it on plotly.

If I set scales = "fixed", I have no problem: the graph looks fine on my computer and on plotly. But if I set scales = "free", as in the code below, then the ggplot2 on my computer looks fine, but the plotly version is not showing up properly (either on my computer or the web).

Here is some sample code; I create some random values for 12 states covering a 16-year period from 2000 to 2015, and I want to plot a time series of those values with each state having its own chart and unique y-axis.

library(ggplot2)
library(plotly)

set.seed(1) 
states = sample(state.name, 12) ## create random list of 12 states 

## create random data with values for state and by year 
dat <- data.frame(Year = rep(2000:2015, times = 12),
              State = rep(states, each = 16),
              Value = rnorm((12*16), mean = 5, sd = 2))

## plot data with facet_wrap for each state
p <- ggplot(dat, aes(Year, Value)) + geom_line() + 
    facet_wrap(~ State, ncol = 4, scales = "free")
p
ggplotly(p)

The ggplot2 command (p) shows up as expected:

But the ggplotly(p) is distorted:

As far as I can tell, there was a previous thread on this topic, but it was not resolved:

Plotly Facets not translating properly

I would appreciate any help on this.

解决方案

If you are open to an alternative, you could try with native plotly with subplot. Try:

dat$id <- as.integer(dat$State)
p <- plot_ly(dat, x = Year, y = Value, group = State,
             xaxis = paste0("x", id), marker=list(color="black"))
p <- layout(
    subplot(p, nrows=3, margin = 0.05),
    xaxis = list(title = "Arizona"),
    xaxis2 = list(title = "Connecticut"),
    xaxis3 = list(title = "Florida"),
    xaxis4 = list(title = "Georgia"),
    xaxis5 = list(title = "Indiana"),
    xaxis6 = list(title = "Maine"),
    xaxis7 = list(title = "Nebraska"),
    xaxis8 = list(title = "Nevada"),
    xaxis9 = list(title = "New Hampshire"),
    xaxis10 = list(title = "South Dakota"),
    xaxis11 = list(title = "Tennessee"),
    xaxis12 = list(title = "Texas"), 

    yaxis = list(title = "Value"),
    yaxis2 = list(title = ""),
    yaxis3 = list(title = ""),
    yaxis4 = list(title = ""),
    yaxis5 = list(title = "Value"),
    yaxis6 = list(title = ""),
    yaxis7 = list(title = ""),
    yaxis8 = list(title = ""),
    yaxis9 = list(title = "Value"),
    yaxis10 = list(title = ""),
    yaxis11 = list(title = ""),
    yaxis12 = list(title = ""), showlegend = FALSE)
p

这篇关于绘制facet_wrap和scales =“free”图时出错在情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆