我希望这个条形图在图表顶部有图例。当我在 Rstudio 中运行以下代码时,图例确实出现在顶部。但是,当我通过 renderPlotly 将其放入 Rshiny 时,图例始终仅位于右侧。有人知道这是为什么以及如何解决这个问题吗?该图在 Rshiny 中显示,如所附照片所示。
[![temp <- structure(list(Cow_No = c(292, 258, 18, 103, 147, 224, 157, 241,
230, 260, 305, 204, 310, 215, 192, 231, 304, 134, 24, 84, 92,
46, 77, 4, 67, 238, 217, 49, 284, 168), core_product = c("Short Gestation Length",
"PS Daughter Proven", "Short Gestation Length", "Short Gestation Length",
"Short Gestation Length", "PS Daughter Proven", "Short Gestation Length",
"Short Gestation Length", "PS Forward Pack", "Nominated", "Short Gestation Length",
"PS Daughter Proven", "PS Daughter Proven", "PS Forward Pack",
"PS Daughter Proven", "PS Daughter Proven", "PS Forward Pack",
"PS Daughter Proven", "Nominated", "Short Gestation Length",
"Short Gestation Length", "Short Gestation Length", "PS Forward Pack",
"Nominated", "Short Gestation Length", "Short Gestation Length",
"Nominated", "Nominated", "Short Gestation Length", "Nominated"
), bw = c(-31.1, -26.5, 21.3, 23, 35.9, 58.2, 63.3, 79.5, 83.9,
83.9, 88.6, 108.9, 118.6, 119.4, 119.5, 119.6, 122.9, 123.7,
128.8, 147.1, 149.4, 149.6, 154.4, 160.6, 167.7, 172.6, 173.4,
181.5, 182.6, 184.3), seq = 1:30), row.names = c(NA, -30L), class = c("tbl_df",
"tbl", "data.frame"))
allprod_chart <- ggplot(temp, aes(x=seq, y = bw, fill=core_product,label=Cow_No)) +
geom_bar(stat="identity") +
ggtitle("ABC") +
scale_fill_manual(values=c( "PS Forward Pack" ="deepskyblue", "Short Gestation Length" ="brown",
"Sire Proving Scheme" = "blue3", "PS Daughter Proven" = "darkblue",
"Nominated"= "brown3", "Sexed Semen" = "darkgreen" ,
"PS A2A2" = "lightblue", "Frozen Sexed" = "lightgreen",
"Unknown" = "grey"
))+
theme_minimal() +
theme( axis.ticks.x=element_blank()
,axis.title = element_text( size = 14, face = "bold" )
, legend.position = "top"
, strip.text = element_text(size = 14, face = "bold")
, plot.title = element_text(size = 20)
)
print(allprod_chart)][1]][1]
我发现了很多有类似问题的帖子: 如何更改 R 中 ggplotly 中的图例位置 Theme(position.legend="none") 不适用于 coord_flip() 如何更改 R 中 ggplotly 中的图例位置 最终,我发现 legend.position 在 ggplotly 中总是“正确”,除非 legend.position =“none”,所以如果我使用 ggplotly 而不是 ggplot,似乎无法解决我的问题。如果我错了请纠正我 https://github.com/plotly/plotly.R/issues/1049