[英]unable to change colors of markers on barplot plotly in a r shiny application
我目前正在開發一個 shiny 應用程序,並且我開始使用 Plotly 來制作一些圖表。 問題是我希望我的條形圖是這樣的:
然而,這是我寫的代碼的結果圖
plot的代碼:
plot_ly(data =gfrq1, x= ~a, y= ~b, source = 'brand',type = 'bar',name = ~is.true, color = ~is.true, colors = c('TRUE' = "#B22222", 'FALSE' = '#808080'))%>%
add_trace(data = gfrq2, x = ~a, y= ~b, type = 'scatter', mode = 'marker+point', name = ~data, marker = list( size =10 , color = ~data,colors = ~data))
數據集是這些:
dput(gfrq1)
structure(list(a = c("person1", "person2", "person3", "person4",
"person5", "person6", "person7", "person8", "person9", "person10",
"person11", "person12", "person13", "person14", "person15"),
b = c(0.452, 0.46, 0.456, 0.444, 0.458, 0.459, 0.446, 0.461,
0.458, 0.459, 0.461, 0.467, 0.467, 0.451, 0.571), is.true = c(FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE)), row.names = c(NA, -15L
), class = "data.frame")
dput(gfrq2)
structure(list(a = c("person1", "person2", "person3", "person4",
"person5", "person6", "person7", "person8", "person9", "person10",
"person11", "person12", "person13", "person14", "person15"),
b = c(0.049109883364027, 0.0478821362799263, 0.0466543891958257,
0.26051282051282, 0.0460405156537753, 0.0466543891958257,
0.262222222222222, 0.0445058317986495, 0.041743400859423,
0.0426642111724985, 0.0441988950276243, 0.049109883364027,
0.0500306936771025, 0.266666666666667, 0.571428571428571),
data = c("data1", "data1", "data1", "data1", "data3", "data3",
"data1", "data2", "data3", "data3", "data1", "data1", "data3",
"data1", "data4"), is.true = c(FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE)), row.names = c(NA, -15L), class = "data.frame")
有什么建議么?
我很驚訝 Plotly 在這方面的不合作。 不幸的是,我不得不承認我偶然發現了解決方案。
我將數據加入到一個數據框中。 不要使用標記的參數color
,而是split
。
我從add_bars
中刪除了參數name
,因為使用該設置,圖例反映了“is.true”和“TRUE”。 (為什么 Plotly 不能讀懂我的想法?)
ggdat <- cbind(gfrq1, gfrq2[, c("b","data")]) # join data
names(ggdat) <- c("a", "b", "is.true", "b2", "dat") # fix names
plot_ly(data = ggdat, x = ~a) %>%
add_bars(y = ~b, color = ~is.true,
colors = c('TRUE' = "#B22222", 'FALSE' = '#808080')) %>%
add_markers(y = ~b2, split = ~dat, marker = list(size = 10))
我很確定沒有markers+point
模式。 對於點、點、散點...等,模式是markers
。 (不過,我可能是錯的!)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.