包shinyscreenshot無法列印顏色plotly條(
目標

圖例中是否有 4 個、5 個或 X 個資料點并不重要。
MWE
library(ggplot2)
library(plotly)
ggplotly(
ggplot(data=mtcars,
aes(x=mpg, y=cyl, color=qsec))
geom_point()
)
uj5u.com熱心網友回復:
Plotly 不會為您創造一個離散的傳奇,但您仍然可以實作它。
首先,我將ggplot和分配給了ggplotly物件。
plt <- ggplotly(
ggplot(data=mtcars,
aes(x=mpg, y=cyl, color=qsec))
geom_point()
)
g <- ggplot(data=mtcars,
aes(x=mpg, y=cyl, color=qsec))
geom_point()
接下來,使用物件背后的資料ggplot,結合mtcars,按qsec資料框獲取顏色,這樣您就知道什么顏色對應什么值。
colByVal <- cbind(ggplot_build(g)$data[[1]], mtcars) %>%
as.data.frame() %>%
select(colour, qsec) %>% arrange(qsec) %>%
group_by(colour) %>%
summarise(qsec = median(qsec)) %>% as.data.frame()
我認為四個或五個值是理想的。我只是用來summary挑選它們。但是,這不是必需的。顯然,您可以選擇任意多個值。這些是我將在圖例中顯示的值。
parts <- summary(colByVal$qsec)
# drop the mean or median (the same color probably)
parts <- parts[-4]
接下來,使用DescTools::Closest查找qsec最接近匯總值的值。
vals <- lapply(parts, function(k) {
DescTools::Closest(colByVal$qsec, k)[1]
}) %>% unlist(use.names = F)
使用這些qsec值和按顏色顯示值的資料框來獲取與這些值關聯的顏色。
cols <- colByVal %>%
filter(qsec %in% vals) %>% select(colour) %>%
unlist(use.names = F)
使用顏色和值(圖例標簽),使用形狀和注釋(圓圈和文本)重建圖例。在每個圖例項之間只有一個其他元素需要更改,即圖例條目的 y 位置。
ys <- seq(from = .7, by = .07, length.out = length(cols))
有兩個功能:形狀和注釋。使用lapply,遍歷這些函式的值、顏色和 y 值,以創建形狀和注釋。
# create shapes
shp <- function(y, cr) { # y0, and fillcolor
list(type = "circle",
xref = "paper", x0 = 1.1, x1 = 1.125,
yref = "paper", y0 = y, y1 = y .025,
fillcolor = cr, yanchor = "center",
line = list(color = cr))
}
# create labels
ano <- function(ya, lab) { # y and label
list(x = 1.13, y = ya .035, text = lab,
xref = "paper", yref = "paper",
xanchor = "left", yanchor = 'top',
showarrow = F)
}
# the shapes list
shps <- lapply(1:length(cols),
function(j) {
shp(ys[j], cols[j])
})
# the labels list
labs <- lapply(1:length(cols),
function(i) {
ano(ys[i], as.character(vals[i]))
})
當您使用ggplotly時,出于某種原因,它以ggplotly物件的空形狀結束。這會干擾呼叫shapesin的能力layout(這是正確的方法)。你必須用形狀來解決問題。此外,圖例欄需要消失。放下圖例欄后,Plotly 將調整繪圖邊距。如果您不添加邊距,使用形狀和注釋創建的圖例將被隱藏。
# ggplot > ggplotly adds an empty shape; this conflicts with calling it in
# layout(); we'll replace 'shapes' first
plt$x$layout$shapes <- shps
plt %>% hide_colorbar() %>%
layout(annotations = labs, showlegend = F,
margin = list(t = 30, r = 100, l = 50, b = 30, pad = 3))

所有這些代碼都在一個塊中:
library(tidyverse)
library(plotly)
# original plot
plt <- ggplotly(
ggplot(data=mtcars,
aes(x=mpg, y=cyl, color=qsec))
geom_point()
)
g <- ggplot(data=mtcars,
aes(x=mpg, y=cyl, color=qsec))
geom_point()
# color by qsec values frame
colByVal <- cbind(ggplot_build(g)$data[[1]], mtcars) %>%
as.data.frame() %>%
select(colour, qsec) %>% arrange(qsec) %>%
group_by(colour) %>%
summarise(qsec = median(qsec)) %>% as.data.frame()
parts <- summary(colByVal$qsec)
# drop the mean or median (the same color probably)
parts <- parts[-4]
vals <- lapply(parts, function(k) {
DescTools::Closest(colByVal$qsec, k)[1]
}) %>% unlist(use.names = F)
cols <- colByVal %>%
filter(qsec %in% vals) %>% select(colour) %>%
unlist(use.names = F)
ys <- seq(from = .7, by = .07, length.out = length(cols))
# create shapes
shp <- function(y, cr) { # y0, and fillcolor
list(type = "circle",
xref = "paper", x0 = 1.1, x1 = 1.125,
yref = "paper", y0 = y, y1 = y .025,
fillcolor = cr, yanchor = "center",
line = list(color = cr))
}
# create labels
ano <- function(ya, lab) { # y and label
list(x = 1.13, y = ya .035, text = lab,
xref = "paper", yref = "paper",
xanchor = "left", yanchor = 'top',
showarrow = F)
}
# the shapes list
shps <- lapply(1:length(cols),
function(j) {
shp(ys[j], cols[j])
})
# the labels list
labs <- lapply(1:length(cols),
function(i) {
ano(ys[i], as.character(vals[i]))
})
# ggplot > ggplotly adds an empty shape; this conflicts with calling it in
# layout(); we'll replace 'shapes' first
plt$x$layout$shapes <- shps
plt %>% hide_colorbar() %>%
layout(annotations = labs, showlegend = F,
margin = list(t = 30, r = 100, l = 50, b = 30, pad = 3))
uj5u.com熱心網友回復:
這只是部分答案;scale_color_continuous您可以通過使用 和 的組合來更改圖例的形狀和文本,同時保持其漸變guides(color = guide_legend()),但這只會顯示為一個ggplot物件。出于某種原因,當您將情節添加到 時,圖??例消失了ggplotly(),圖例消失了。我懷疑可能需要ggplotly()直接添加特定于圖例的引數。
library(tidyverse)
library(plotly)
data(mtcars)
p <- ggplot(mtcars, aes(x = mpg, y = cyl, color = qsec))
geom_point()
scale_color_continuous(breaks = c(15, 17.5, 20, 22.5))
guides(color = guide_legend(
reverse = T,
override.aes = list(shape = 19, size = 8)))
theme(legend.position = "right")
p
p2 <- ggplotly(p) %>% layout(showlegend = T)
p2
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/536933.html
標籤:r图表2巧妙地
上一篇:條形圖中錯誤條的錯誤位置
