简体   繁体   中英

R Plotly: Sort x-axis by categorical variable for scatter plot

I want to manually sort the x-axis for an R plotly scatterplot using crosstalk to filter by a code name (Code) but my x-axis is a time-series categorical variable (Time; ie Jan-Feb 2019, Mar-Apr 2019). By default, R sorts the x-axis alphabetically, which put my x-axis out of order and the points are jumbled (ie the first point connects to the last point which then connects to the fourth point...)

I tried converting Time to a factor and manually setting the order, but the points on the graph are still jumbled

Any help is much appreciated!

#Create dataset
df <- data.frame(
  "Code" = c("A","A","A","A","A","B","B","B","B","B"),
  "Time" = c("2016-2018","Jan-Feb 2019","Mar-Apr 2019","May-Jun 2019","Jul-Aug 2019", "2016-2018","Jan-Feb 2019","Mar-Apr 2019","May-Jun 2019","Jul-Aug 2019"),
  "Rate" = c(40.8, 50.8, 15.3, 39, 40.1, 70.2, 38.3, 25.2, 46.7, 41.9)
)

#Create filter
filter <- SharedData$new(df)
filter_select("Code", "Select Provider", filter, ~Code, multiple = FALSE)

#Graph
filter %>%
  plot_ly(
    x = ~`Time`,
    y = ~`Rate`,
    type = "scatter", 
    mode="lines+markers"
  )

#ATTEMPT 1: Convert Time to factor, rerun filter, then graph
df$Time <- factor(df$Time, levels = c("2016-2018", "Jan-Feb 2019", "Mar-Apr 2019", "May-Jun 2019", "Jul-Aug 2019"))

filter <- SharedData$new(df)
filter_select("Code", "Select Provider", filter, ~Code, multiple = FALSE)

plot_ly(df,
  x = ~`Time`,
  y = ~`Rate`,
  type = "scatter", 
  mode="lines+markers"
)

If you call plot_ly on the SharedData object rather than the original dataframe, you can use the filter_select option to show Code A and B in the correct order:

bscols(filter_select("Code", "Select Provider", filter, ~Code, multiple = FALSE), 
        plot_ly(filter,
                         x = ~`Time`,
                        y = ~`Rate`,
                      type = "scatter", 
                      mode="lines+markers"
              ))

Your problem is that your Time column is already a factor . If you set stringsAsFactors = FALSE when setting up the data frame, your code will work. I would also add a color option in plotly .

df <- data.frame(
  "Code" = c("A","A","A","A","A","B","B","B","B","B"),
  "Time" = c("2016-2018","Jan-Feb 2019","Mar-Apr 2019","May-Jun 2019","Jul-Aug 2019", "2016-2018","Jan-Feb 2019","Mar-Apr 2019","May-Jun 2019","Jul-Aug 2019"),
  "Rate" = c(40.8, 50.8, 15.3, 39, 40.1, 70.2, 38.3, 25.2, 46.7, 41.9),
  stringsAsFactors = FALSE
)
df$Time <- factor(df$Time, levels = c("2016-2018", "Jan-Feb 2019", "Mar-Apr 2019",
                                      "May-Jun 2019", "Jul-Aug 2019"))
#Create filter
filter <- SharedData$new(df)
filter_select("Code", "Select Provider", filter, ~Code, multiple = FALSE)

#Graph
filter %>%
  plot_ly(
    x = ~`Time`,
    y = ~`Rate`,
    color = ~`Code`,
    type = "scatter", 
    mode="lines+markers"
  )

例

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