簡體   English   中英

在R中使用圖繪制氣泡圖時的訂購軸

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

我有一個在R中使用plotly的氣泡圖,但軸的順序似乎有些奇怪。

輸出如下,可以看到軸不正確:

泡泡圖

我正在使用的代碼如下

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)')

數據如下:

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"))

關於如何正確訂購軸的任何想法?

謝謝

有幾種方法可以控制因子水平,但是如果不小心,情況可能會變得有些混亂。 你應該熟悉?levels?factor ,以及可能?reorder?relevel

在此期間,嘗試類似這樣的操作

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

編輯

為了鞏固我的回答和評論...

導致這種現象的原因是編碼因素的方式。 您的軸是字符串,並且因子順序由字母數字確定。 因此,要更改它們的順序,您必須如上所述指定它,或者對它們進行數字編碼並為其指定所需的名稱。 在多個軟件包中,有許多不同的更改方法。 該答案為處理因子提供了標准的基本R方法。 有關更多信息,請從我建議的手冊頁開始。

至於“非常手冊”,由於因子是分類的(因此具有潛在的任意順序),除非您按所需的順序對它們進行數字編碼,否則無法自動執行其順序。

感謝上面的評論,我已經能夠解決問題。 在下面找到完整的代碼,希望對其他用戶有所幫助:

    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