[英]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.