我能夠創建我想要的圖表作為標準ggplot2
data <- structure(list(Date = structure(c(19205, 19236, 19266, 19205,
19236, 19266, 19205, 19236, 19266, 19205, 19236, 19266, 19205,
19236, 19266, 19205, 19236, 19266, 19205, 19236, 19266, 19205,
19236, 19266, 19205, 19236, 19266), class = "Date"), Key = c("Average Hourly Earnings",
"Average Hourly Earnings", "Average Hourly Earnings", "Case Shiller House Price Index",
"Case Shiller House Price Index", "Case Shiller House Price Index",
"HPA", "HPA", "HPA", "Industrial Production", "Industrial Production",
"Industrial Production", "Personal Consumption Expenditures",
"Personal Consumption Expenditures", "Personal Consumption Expenditures",
"Retail Sales", "Retail Sales", "Retail Sales", "Capacity Utilisation",
"Capacity Utilisation", "Capacity Utilisation", "Total Vehicle Sales",
"Total Vehicle Sales", "Total Vehicle Sales", "Construction Spending",
"Construction Spending", "Construction Spending"), Value = c(-0.223065575886151,
-1.40315318073944, -1.91465670559936, -2.51599837113228, NA,
NA, -2.39639074829503, NA, NA, -0.620070909725157, 0.438262897380392,
-1.02195804568898, -1.18062194826764, -1.04966383725284, NA,
-0.434581634758551, -0.671474725212731, -0.689827602986322, 0.814973303786158,
0.792591156917412, 0.36832537554409, 2.29199981656987, 2.23432584415557,
1.83201171904481, 0.283236872921727, 0.721164674024409, NA),
Color = c("#e6cc00", "#ff0000", "#ff0000", "#7b0000", NA,
NA, "#7b0000", NA, NA, "#e6cc00", "#ece75f", "#ff0000", "#ff0000",
"#ff0000", NA, "#e6cc00", "#e6cc00", "#e6cc00", "#ece75f",
"#ece75f", "#ece75f", "#0a5d00", "#0a5d00", "#0eff00", "#ece75f",
"#ece75f", NA)), row.names = c(NA, -27L), class = c("tbl_df",
"tbl", "data.frame"))
library(tidyverse)
library(plotly)
p <- data2 %>%
{ggplot(.,aes(Date, Key, fill = Color))
geom_tile()
theme_light()
scale_fill_identity(guide = 'legend', breaks = c('NA','#0a5d00','#0eff00', "#ece75f",'#e6cc00','#ff0000','#7b0000'),
labels = c("NA",'> 2 Standard Deviations from mean', 'Between 1 and 2 Standard Deviations from mean',
'Between 0 and 1 Standard Deviations from mean', 'Between 0 and -1 Standard Deviations from mean',
'Between -1 and -2 Standard Deviations from mean', '> -2 Standard Deviations from mean'))
}
p

但是,當我嘗試使用 將其轉換為互動式圖表ggplotly時,它忽略了我在ggplot. 有沒有辦法像我在我的 中那樣手動設定圖例項的順序以及圖例中的標簽ggplot?
p %>% ggplotly(tooltip = c('Date','Key')) %>%
layout(legend = list(orientation = "h", x = 0.4, y = -0.2))

uj5u.com熱心網友回復:
為了實作這一點,我在您的資料框中創建了另一個變數。我將您最初通話中的中斷和標簽復制到了scale_fill_manual.
我想指出的是,在查看器窗格中,如果視窗不夠大,無法水平反映標簽,則圖例將反映為堆疊。
新變數。
data2 <- data2 %>%
mutate(labs = Color %>% as.factor() %>% plyr::mapvalues(
# copied from breaks; NA removed
from = c('#0a5d00','#0eff00',"#ece75f",'#e6cc00','#ff0000','#7b0000'),
# copied from labels; NA removed
to = c('> 2 Standard Deviations from mean',
'Between 1 and 2 Standard Deviations from mean',
'Between 0 and 1 Standard Deviations from mean',
'Between 0 and -1 Standard Deviations from mean',
'Between -1 and -2 Standard Deviations from mean',
'> -2 Standard Deviations from mean')))
對繪圖的修改——設定,過濾掉 NA,更改fill為fill = labs,而不是scale_fill_identity,我使用了scale_fill_manual。在該呼叫中,您會將顏色映射到顏色標簽。(而不是相反,這是您最初所做的。)
(p <- data2 %>% filter(!is.na(Color)) %>%
ggplot(.,aes(Date, Key, fill = labs))
geom_tile()
theme(legend.position = "bottom")
scale_fill_manual(values = levels(as.factor(data2$Color)),
breaks = levels(as.factor(data2$labs))))
我有 2 個不同的版本供您將其視為 Plotly 物件。第一個將圖例居中(只是網格區域)。第二個是左對齊。
p %>% ggplotly() %>%
layout(legend = list(xanchor = "center", y = -.2,
x = .5, orientation = "h"))
p %>% ggplotly() %>%
layout(legend = list(xanchor = "left", y = -.2,
x = -.2, orientation = "h"))
第一張圖片是居中的變化;第二個是左對齊版本。


僅供參考:如果您偏愛標簽在ggplot(y 軸)中提供的順序,如果您將它們設為有序因子,則可以強制 Plotly 尊重它們。
您原來的圖例標題是Color. 如果您希望它成為該圖的圖例標題,只需將其添加到layout呼叫中即可。
p %>% ggplotly() %>%
layout(legend = list(xanchor = "left", y = -.2,
x = -.2, orientation = "h",
title = list(text = "Color")))

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/536374.html
標籤:r图表2巧妙地
