我尝试将+ guides(fill = guide_legend(nrow = 1))
添加到我的ggplot
,但这显然仅适用于缩放的图例(如果我错了,请纠正我)。这是我的示例代码:
library(ggplot2)
library(lubridate) # because my dates are Date objects
df = structure(list(date = structure(c(16829, 16860, 16891, 16920, 16952, 16982, 17011, 17044, 17074, 17105, 17135, 17165, 17197, 17225, 17256, 17284, 16829, 16860, 16891, 16920, 16952, 16982, 17011, 17044, 17074, 17105, 17135, 17165, 17197, 17225, 17256, 17284, 16829, 16860, 16891, 16920, 16952, 16982, 17011, 17044, 17074, 17105, 17135, 17165, 17197, 17225, 17256, 17284, 16829, 16860, 16891, 16920, 16952, 16982, 17011, 17044, 17074, 17105, 17135, 17165, 17197, 17225, 17256, 17284, 16829, 16860, 16891, 16920, 16952, 16982, 17011, 17044, 17074, 17105, 17135, 17165, 17197, 17225, 17256, 17284, 16829, 16860, 16891, 16920, 16952, 16982, 17011, 17044, 17074, 17105, 17135, 17165, 17197, 17225, 17256, 17284), class = "Date"), metric = c("Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q4", "Q4", "Q4", "Q4", "Q4", "Q4", "Q4", "Q4", "Q4", "Q4", "Q4", "Q4", "Q4", "Q4", "Q4", "Q4", "Q5", "Q5", "Q5", "Q5", "Q5", "Q5", "Q5", "Q5", "Q5", "Q5", "Q5", "Q5", "Q5", "Q5", "Q5", "Q5", "long.short", "long.short", "long.short", "long.short", "long.short", "long.short", "long.short", "long.short", "long.short", "long.short", "long.short", "long.short", "long.short", "long.short", "long.short", "long.short"), value = c(1.43328462686754, 1.54489707332386, 1.70214055968945, 1.80207323194882, 1.80597922567907, 1.78875018386609, 1.9034761487838, 1.9594288301773, 2.00085605421932, 1.98716239058971, 2.09707234241323, 2.19145632586439, 2.16323036838726, 2.15502118647646, 2.2119999466469, 2.20324042685817, 1.99174546301834, 2.01769433147371, 2.12390265693736, 2.19861093289513, 2.30975353428495, 2.2950777156748, 2.47832495923479, 2.46124358105422, 2.45986528464883, 2.40346672333504, 2.44820201806502, 2.56983371627534, 2.49459689224347, 2.57941958298203, 2.70048034207666, 2.73621669860347, 2.09834856980166, 2.06279260548336, 2.17264424554307, 2.22186006931523, 2.28463046481194, 2.25515287378903, 2.42685674413337, 2.4310072914368, 2.45504309686187, 2.31418949011192, 2.3305015591076, 2.42966738827014, 2.39754111122874, 2.46807800022935, 2.53119118457881, 2.56551154094818, 1.98360798055642, 1.98145144264935, 2.09636546503356, 2.11230832439514, 2.23638748184019, 2.13276819518159, 2.31461129268462, 2.33436264238219, 2.34370592885833, 2.17472473138764, 2.1505351009139, 2.24138693763866, 2.16546139191265, 2.16695500502656, 2.26068414343628, 2.31511446165902, 0.767009066338262, 0.780594960262018, 0.806616093262352, 0.829044053735512, 0.876204224732257, 0.85569885536296, 0.933017664686419, 0.894931883613919, 0.87666408653965, 0.841467092567628, 0.821440175764518, 0.835441623560424, 0.809668249473585, 0.838261683703745, 0.875962502928321, 0.885138210146495, 0.541391910395761, 0.508822328340106, 0.473994712021047, 0.459345905446036, 0.48448016502728, 0.477764058739589, 0.490291032359741, 0.455865247522602, 0.4369217671618, 0.422370140989619, 0.38895643913593, 0.378080244706592, 0.371286142709214, 0.385807096149784, 0.39295803067692, 0.398630379849741)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -96L), .Names = c("date", "metric", "value"))
ggplot(df, aes(date, value, colour = metric)) +
theme(legend.position = "bottom", axis.title.x = element_blank(), axis.title.y = element_blank(), legend.title = element_blank()) +
geom_line()
控制图例行数和/或列数的正确方法是什么?
答案 0 :(得分:3)
您尝试修改fill
图例,但是您有colour
比例,所以:
ggplot(df, aes(date, value, colour = metric)) +
theme(legend.position = "bottom", axis.title.x = element_blank(), axis.title.y = element_blank(), legend.title = element_blank()) +
geom_line() +
guides(colour = guide_legend(nrow = 1))