Как показать вторую легенду для третьей переменной в точечной диаграмме, используя plotly? - PullRequest
1 голос
/ 18 апреля 2020

У меня есть следующий код, создающий точечную диаграмму (используя в ), и я хотел бы добавить , представляющий размер маркеров totalValue (это непрерывная переменная, представляющая значение указанных c зданий), как я могу это сделать?

 f <- list(
   family = "Courier New, monospace",
   size = 18,
   color = "#7f7f7f"
  )
 x <- list(
   title = "Age of Buildings",
   titlefont = f,
   zeroline = FALSE,
   showline = FALSE,
   showticklabels = TRUE,
   showgrid = TRUE
  )
  y <- list(
    title = "Total Violations",
    titlefont = f,
    zeroline = FALSE,
    showline = FALSE,
    showticklabels = TRUE,
    showgrid = TRUE
   )
fig2 <- plot_ly(final, x=~agebuilding, y=~violationstotal, mode= "markers", color = 
                ~INdexrehabless6, size = ~totalvalue)
fig2 <- fig2 %>% layout(xaxis = x, yaxis = y, legend=list(title=list(text='<b> 
                        Housing Conditions </b>'))) #chaging name legend
fig2

образец набора данных:

agebuilding  violationstotal  INdexrehabless6  totalvalue
32            5                0                 350000
120          15                1                  50000
100          25                1                 100000
32           31                0                 210000
33            9                0                 150000
50           20                0                 301000
15           28                0                 175000
70           18                1                 125000

Вот Сюжет получаю

enter image description here

1 Ответ

0 голосов
/ 19 апреля 2020

Насколько мне известно, заговор с двумя легендами в пока невозможен:

Даже с (с широкими возможностями настройки) и (обертка вокруг ), в конечном итоге приводит только к одной легенде.

library(ggplot2); library(dplyr); library(magrittr);

final <- read.table(header = T, text = 
                        "agebuilding  violationstotal  INdexrehabless6  totalvalue
32            5                0                 350000
120          15                1                  50000
100          25                1                 100000
32           31                0                 210000
33            9                0                 150000
50           20                0                 301000
15           28                0                 175000
70           18                1                 125000"); 

final;
  agebuilding violationstotal INdexrehabless6 totalvalue
1          32               5               0     350000
2         120              15               1      50000
3         100              25               1     100000
4          32              31               0     210000
5          33               9               0     150000
6          50              20               0     301000
7          15              28               0     175000
8          70              18               1     125000

#plots the ggplot in `plot` window. 1st GRAPH
ggplot(data = final, aes(x = agebuilding, y = violationstotal)) + 
    geom_point(aes(size = totalvalue, color = INdexrehabless6))

# plots the ggploty in viewer window. 2nd GRAPH (interactive)
ggp %>% ggplotly();

1-й ГРАФ enter image description here

2-й ГРАФ enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...