简体   繁体   English

在R中使用图绘制气泡图时的订购轴

[英]Order axis when doing a bubble chart using plotly in R

I have a bubble chart using plotly in R but the order of the axis appear to be somehow odd. 我有一个在R中使用plotly的气泡图,但轴的顺序似乎有些奇怪。

The output is as follows and you can see how the axis are not correct: 输出如下,可以看到轴不正确:

泡泡图

The code that I'm using is as follows 我正在使用的代码如下

library(plotly)
library(ggplot2)

file <- c("C://link//data.csv")
#dataSource <- read.csv(file, sep =",", header = TRUE)
dataSource <- read.table(file, header=T, sep=",")
dataSource <- na.omit(dataSource)

slope <- 1 
dataSource$size <- sqrt(dataSource$Y.1 * slope)
colors <- c('#4AC6B7', '#1972A4') #, '#965F8A', '#FF7070', '#C61951')

plot_ly(dataSource, 
        x = ~Y.1.vs.Y.2, 
        y = ~YTD.vs.Y.1.YTD, 
        color = ~BU, 
        size = ~size, 
        colors = colors, 
        type = 'scatter', 
        mode = 'markers', 
        sizes = c(min(dataSource$size), max(dataSource$size)),
        marker = list(symbol = 'circle', sizemode = 'diameter',
        line = list(width = 2, color = '#FFFFFF')),
        text = ~paste('Business Unit:', 
                      BU, '<br>Product:', 
                      Product, '<br>Y.1.vs.Y.2:', 
                      Y.1.vs.Y.2, '<br>YTD.vs.Y.1.YTD:', 
                      YTD.vs.Y.1.YTD)) %>%
        layout(title = 'Y.1.vs.Y.2 v. YTD.vs.Y.1.YTD',
                      xaxis = list(title = 'Y.1.vs.Y.2',
                      gridcolor = 'rgb(255, 255, 255)',
                      zerolinewidth = 1,
                      ticklen = 5,
                      gridwidth = 2),
         yaxis = list(title = 'YTD.vs.Y.1.YTD',
                      gridcolor = 'rgb(255, 255, 255)',
                      zerolinewidth = 1,
                      ticklen = 5,
                      gridwith = 2),
         paper_bgcolor = 'rgb(243, 243, 243)',
         plot_bgcolor = 'rgb(243, 243, 243)')

The data is as follows: 数据如下:

structure(list(BU = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("B", "D"), class = "factor"), Product = structure(c(4L, 5L, 7L, 8L, 9L, 13L, 1L, 3L, 4L, 11L, 12L, 13L), .Label = c("ADT", "BHL", "CEX", "CMX", "CTL", "HTH", "MTL", "SSL", "TLS", "UTV", "WEX", "WLD", "WMX"), class = "factor"), Y.2 = c(4065L, 499L, 20L, 5491L, 781L, 53L, 34L, 1338L, 557L, 428L, 310L, 31L), Y.1 = c(4403L, 550L, 28L, 5225L, 871L, 46L, 22L, 1289L, 602L, 426L, 318L, 37L), Y.1.YTD = c(4403L, 550L, 28L, 5225L, 871L, 46L, 22L, 1289L, 602L, 426L, 318L, 37L), YTD = c(5026L, 503L, 29L, 3975L, 876L, 40L, 62L, 1395L, 717L, 423L, 277L, 35L), Y.1.vs.Y.2 = structure(c(12L, 7L, 11L, 4L, 8L, 1L, 2L, 3L, 12L, 6L, 10L, 9L), .Label = c("-13%", "-35%", "-4%", "-5%", "-76%", "0%", "10%", "12%", "19%", "3%", "40%", "8%"), class = "factor"), YTD.vs.Y.1.YTD = structure(c(8L, 5L, 11L, 3L, 7L, 2L, 9L, 12L, 10L, 1L, 2L, 4L), .Label = c("-1%", "-13%", "-24%", "-5%", "-9%", "0%", "1%", "14%", "182%", "19%", "4%", "8%"), class = "factor")), .Names = c("BU", "Product", "Y.2", "Y.1", "Y.1.YTD", "YTD", "Y.1.vs.Y.2", "YTD.vs.Y.1.YTD"), row.names = c(2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 13L, 14L, 15L), class = "data.frame", na.action = structure(c(1L, 7L, 12L), .Names = c("1", "7", "12"), class = "omit"))

Any ideas on how can I order the axis properly? 关于如何正确订购轴的任何想法?

Thanks 谢谢

There are a few ways to manipulate factor levels, but things can get a bit messy if you're not careful. 有几种方法可以控制因子水平,但是如果不小心,情况可能会变得有些混乱。 You should familiarize yourself with ?levels and ?factor , as well as maybe ?reorder , ?relevel 你应该熟悉?levels?factor ,以及可能?reorder?relevel

In the meantime, try something like this 在此期间,尝试类似这样的操作

dataSource[[7]] <- factor(dataSource[[7]], levels = c("-76%", "-35%", "-13%", "-5%", "-4%", "0%", "3%", "8%", "10%", "12%", "19%", "40%"))

Edit 编辑

To consolidate my answer and comment... 为了巩固我的回答和评论...

This behaviour is caused because of the way factors are encoded. 导致这种现象的原因是编码因素的方式。 Your axes are strings and factor order is determined alphnumerically. 您的轴是字符串,并且因子顺序由字母数字确定。 So to change their order you have to specify it as above, or else code them numerically and give them the required names. 因此,要更改它们的顺序,您必须如上所述指定它,或者对它们进行数字编码并为其指定所需的名称。 There are many different ways to change them, in several packages. 在多个软件包中,有许多不同的更改方法。 This answer provides a standard base R method for handling factors. 该答案为处理因子提供了标准的基本R方法。 For further info start with the manual pages I suggested. 有关更多信息,请从我建议的手册页开始。

As for it being "very manual", since factors are categorical (and therefore have a potentially arbitrary order), there is no way to automate their order unless you code them numerically in the desired order. 至于“非常手册”,由于因子是分类的(因此具有潜在的任意顺序),除非您按所需的顺序对它们进行数字编码,否则无法自动执行其顺序。

Thanks to the comments above I've been able to resolve the issue. 感谢上面的评论,我已经能够解决问题。 Find below the full code, which I hope might help other users: 在下面找到完整的代码,希望对其他用户有所帮助:

    library(plotly)
    library(ggplot2)

    file <- c("C://link//data.csv")
    dataSource <- read.table(file, header=T, sep=",")
    dataSource <- na.omit(dataSource)

    # Additional code to format the input values and recalculate the percentages
    BUValues = dataSource$BU
    ProductValues = dataSource$Product
    dataSource <- as.data.frame(data.matrix(dataSource), stringsAsfactors = FALSE)
    dataSource$BU = BUValues
    dataSource$Product = ProductValues
    dataSource$Y.1.vs.Y.2 = round((dataSource$Y.1/dataSource$Y.2 -1)*100,2)
    dataSource$YTD.vs.Y.1.YTD = round((dataSource$YTD/dataSource$Y.1.YTD -1)*100,2)

    slope <- 1 
    dataSource$size <- sqrt(dataSource$Y.1 * slope)
    colors <- c('#4AC6B7', '#1972A4') #, '#965F8A', '#FF7070', '#C61951')

    plot_ly(dataSource, 
            x = ~Y.1.vs.Y.2, 
            y = ~YTD.vs.Y.1.YTD, 
            color = ~BU, 
            size = ~size, 
            colors = colors, 
            type = 'scatter', 
            mode = 'markers', 
            sizes = c(min(dataSource$size), max(dataSource$size)),
            marker = list(symbol = 'circle', sizemode = 'diameter',
            line = list(width = 2, color = '#FFFFFF')),
            text = ~paste('Business Unit:', BU, 
                          '<br>Product:', Product, 
                          '<br>YoY:',Y.1.vs.Y.2, 
                          '<br>YTD:',YTD.vs.Y.1.YTD)) %>%
            layout(title = 'YoY vs YTD Performance',
                          xaxis = list(title = 'YoY Performance (%)',
                          gridcolor = 'rgb(255, 255, 255)',
                          zerolinewidth = 1,
                          ticklen = 5,
                          gridwidth = 2),
                          yaxis = list(title = 'YTD Performance (%)',
                          gridcolor = 'rgb(255, 255, 255)',
                          zerolinewidth = 1,
                          ticklen = 5,
                          gridwith = 2),
             paper_bgcolor = 'rgb(243, 243, 243)',
             plot_bgcolor = 'rgb(243, 243, 243)')

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

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