我有雙軸條形圖,以便可視化 3 個數值變數。所有這些在 ggplot 中都很好用。但是,當我將 ggplot 轉換為ggplotly時,會出現問題:
- 在傳說中,有奇怪的1(以黃色突出顯示)
- 在懸停中,有雙值(以黃色突出顯示)
- geom_text中hjust=0, vjust =-0.5的變化不會反映在繪圖上
有人可以幫我調整這些問題嗎?
df <- data.frame (model = c("A", "B", "C","D","E","F"),
share = c(12,20,15,9,60,20),
sale = c(16,25,18,14,67,28),
cost = c(14,19,28,24,57,28))
#set levels of model by cost
df$model <- factor(df$model, levels = arrange(df, desc(df$cost))$model)
library(tidyverse)
df_long <- df %>%
pivot_longer(
cols = -model
)
plt <- ggplot(df_long, aes(x = model, y= value, label=value))
geom_col(data = filter(df_long, name != "cost"), aes(fill=name), position = position_dodge())
scale_fill_manual(values = c("blue", "grey"))
geom_line(data = filter(df_long, name == "cost"), aes(color = name, group = 1), size = 1)
scale_color_manual(values = "red")
geom_text(data = filter(df_long, name == "cost"), size = 3,hjust=0, vjust=-0.5)
geom_label(data = filter(df_long, name == "cost"), hjust=0, vjust=-0.5)
scale_y_continuous(
name = "Sale and Share",
sec.axis = sec_axis(~., name ="Cost")
)
theme_minimal()
theme(legend.title=element_blank())
ggplotly(plt)
uj5u.com熱心網友回復:
到目前為止,您似乎已經掌握了一些重要資訊。這解決了您確定的所有問題。雖然,在這一點上,在 Plotly 中制作情節會容易得多!
我做的第一件事是注釋掉對geom_text
and的呼叫geom_label
。Plotly 在這里的表現并不好。它正在回到情節中,但不是在這里。
接下來,我構建了您的繪圖并查看了轉換分配的名稱和圖例組。這并沒有改變任何東西——這只是在尋找。
plt2 <- plotly_build(plt)
invisible(
lapply(1:length(plt2$x$data),
function(j) {
message(j, " ", plt2$x$data[[j]]$name, " & ",
plt2$x$data[[j]]$legendgroup)
})
)
# 1 (sale,1) & (sale,1)
# 2 (share,1) & (share,1)
# 3 (cost,1) & (cost,1)
@Quinten 解決了這個問題,但這就是你的樣子。一旦我看到 Plotly “做出”了什么,我確信我知道我需要改變什么。
此代碼更改這些字串。它還將更新列印到控制臺,以便您檢查您的期望。
invisible(
lapply(1:length(plt2$x$data),
function(j) {
x <- plt2$x$data[[j]]$name # find the name
y <- str_extract(x, "[a-z] ") # remove anything that's not a letter
plt2$x$data[[j]]$name <<- y # put it back
plt2$x$data[[j]]$legendgroup <<- y
message(j, " ", plt2$x$data[[j]]$name, " & ",
plt2$x$data[[j]]$legendgroup)
})
)
# 1 sale & sale
# 2 share & share
# 3 cost & cost
您也可以使用這種查看/更改/檢查來驗證最終出現在工具提示中的資訊。而不是$name
or $legendgroup
,您將查看$text
.
下一段代碼不會檢查輸入并將其列印出來(我認為這將是多余的)。這只是改變它。(不過,我確實使用該程序來構建它。)
tx = " "
invisible(
lapply(1:length(plt2$x$data),
function(k){
tx <<- plt2$x$data[[k]]$text # tooltip for each trace
lapply(1:length(tx),
function(m) {
tr <- strsplit(tx[[m]], "<br />") # tooltip for each point
tr2 <- unique(tr[[1]]) # remove redundancy
str <- paste0(tr2, collapse = "<br />")
tx[[m]] <<- str # put it back together
})
plt2$x$data[[k]]$text <<- tx # change the plot
})
)
現在到標簽上——如果你想要背景或邊框,你必須annotations
在 Plotly 中使用。就像annotation
在 ggplot 包中一樣, annotations
Plotly 本身的“規則”較少。
你有一個奇怪的訂單model
,所以也必須解決這個問題。當資料在 ggplot 和 Plotly 之間移動時,事情往往會出錯。因此,您不太可能連接到原始資料。
需要記住的一件事是,我將圖紙空間用于 x 軸。Plotly 中的默認圖紙空間(域)為 [0,1]。您的圖表沿 x 均勻分布,您的值位于六個類別的中間,因此 x 上的所有內容都以 1/6 空間表示。
因此,首先,將資料按需要出現在圖中的順序排列。然后將注釋(標簽)添加到圖中。我還在這里洗掉了圖例的名稱。
# to add labels, we need to have the order the data appears on the plot
df2 <- df_long %>%
arrange(desc(value)) %>%
filter(name == "cost")
plt2 %>%
layout(legend = list(title = "")) %>% # remove legend name
add_annotations(x = c(1/12, 1/6 1/12, 1/3 1/12, # using domain for x-axis
1/2 1/12, 2/3 1/12, 5/6 1/12),
y = df2$value,
text = df2$value,
xshift = 20, # shift right 20 px
yshift = 15, # shift up 15 px
hoverinfo = "skip",
bgcolor = "white",
bordercolor = "black",
xref = "paper", yref = "y", # cat x, use domain for annot x
showarrow = F)
畢竟,這是你的情節。
這是直截了當的。我認為帶有填充(可以添加)的標簽看起來會更好一些。
df_long %>%
filter(name != "cost") %>%
plot_ly(x = ~model, y = ~value, color = ~name, type = "bar",
customdata = ~name, colors = c("blue", "gray"),
hovertemplate = paste0("Model: %{x}<br>Value: %{y}<br>",
"Name: %{customdata}<extra></extra>")) %>%
add_lines(inherit = F, data = df, x = ~model,
y = ~cost, color = I("red"),
name = "cost",
hovertemplate = paste0("Model: %{x}<br>Value: %{y}<br>",
"Name: cost<extra></extra>")) %>%
add_annotations(data = df, x = ~model, y = ~cost, text = ~cost,
bgcolor = "white", bordercolor = "black",
xshift = 15, yshift = 15, showarrow = F) %>%
layout(barmode = "group")
與轉換后的情節幾乎相同。
uj5u.com熱心網友回復:
傳說問題:
使用這篇文章中的代碼:
uj5u.com熱心網友回復:
@Quinten 回答的進一步方法,
處理'name name'和'value value'的事情,
嘗試
tooltip = c("value", "name", "model")
plt1$x$layout$legend$title$text <- "name"
完整代碼是
plt <-
ggplot(df_long, aes(x = model, y= value, label = NA))
geom_col(data = filter(df_long, name != "cost"), aes(fill=name), position = position_dodge())
scale_fill_manual(values = c("blue", "grey"))
geom_line(data = filter(df_long, name == "cost"), aes( group = 1, color = name), size = 1)
scale_color_manual(values = "red")
#geom_text(data = filter(df_long, name == "cost"), size = 3,hjust=0, vjust=-0.5)
geom_label(data = filter(df_long, name == "cost"), hjust=0, vjust=-0.5)
scale_y_continuous(
name = "Sale and Share",
sec.axis = sec_axis(~., name ="Cost")
)
theme_minimal()
plt1 <- ggplotly(plt, tooltip = c("value", "name", "model"))
for (i in 1:length(plt1$x$data)){
if (!is.null(plt1$x$data[[i]]$name)){
plt1$x$data[[i]]$name = gsub("\\(","",str_split(plt1$x$data[[i]]$name,",")[[1]][1])
}
}
plt1$x$layout$legend$title$text <- "name"
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/488010.html
標籤:r ggplot2 阴谋 tidyverse 数据可视化
上一篇:一個繪圖中的多個圖形ggplot