2016-04-13 5 views
1

ben R.R satırında Çizelge grafiğinin hoverinfo'undaki boyut çizgisini nasıl kaldırabilirim? Ben kodu kopyalayıp zaman

https://plot.ly/r/text-and-annotations/#custom-hover-text

Bu tam istediğim şeyi yapmak gibi görünüyor

yılında plotly grafikler için özel vurgulu bir metin oluşturmak için nasıl talimat aşağıdaki sayfayı buldum, ancak (aşağıya bakınız) RStudio içine ve yerel olarak çalıştırmak ben hoverinfo, boyut değişkenini gösteren fazladan bir satır olsun. RStudio grafiğin

Ekran Görüntüsü: hoverinfo satırı:

enter image description here

Nasıl bu "1,835 wt (boyutu)" kaldırabilirim?

library(plotly) 
p <- mtcars %>% 
    plot_ly(x = disp, y = mpg, mode = "markers", color = cyl, size = wt, 
      hoverinfo = "text", 
      text = paste("Displacement = ", mtcars$disp, "Miles Per Gallon = ", mtcars$mpg)) %>% 
    layout(title ="Custom Hover Text") 
p 

cevap

3

Ne istediğinizi başarabilirim, ancak çirkin ve gerçekten de bir kesmek. Bundan fazlasıyla gurur duymuyorum ama işte başlıyoruz.

# Your plot 
library(plotly) 
p <- mtcars %>% 
    plot_ly(x = disp, y = mpg, mode = "markers", color = cyl, size = wt, 
      hoverinfo = "text", 
      text = paste("Displacement = ", mtcars$disp, "Miles Per Gallon = ", mtcars$mpg)) %>% 
    layout(title ="Custom Hover Text") 
p 

# Get the list for the plot 
pp <- plotly_build(p) 

# Pick up the hover text 
hvrtext <- pp$data[[1]]$text 

# Split by line break and wt 
hvrtext_fixed <- strsplit(hvrtext, split = '<br>wt') 

# Get the first element of each split 
hvrtext_fixed <- lapply(hvrtext_fixed, function(x) x[1]) 

# Convert back to vector 
hvrtext_fixed <- as.character(hvrtext_fixed) 

# Assign as hovertext in the plot 
pp$data[[1]]$text <- hvrtext_fixed 

# Plot 
pp 
1

Buraya aynı çözümü aramak için geldim ve yukarıdakiler biraz haggle sonra çalıştı ama sonuçta daha sonra doğru yöntemi buldum. İşte burada:

işaretleyici içinde senin 'Boyut' değişkeni koyun = liste()

Bunun yerine

plot_ly(x = disp, y = mpg, mode = "markers", color = cyl, size = wt, 
      hoverinfo = "text", 
      text = paste("Displacement = ", mtcars$disp, "Miles Per Gallon = ", mtcars$mpg)) 

ait kullanabilirsiniz

plot_ly(x = disp, y = mpg, mode = "markers", color = cyl, marker=list(size=wt), 
       hoverinfo = "text", 
       text = paste("Displacement = ", mtcars$disp, "Miles Per Gallon = ", mtcars$mpg)) 

Bu benim için çalıştı.

İlgili konular