简体   繁体   中英

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.

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

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

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