我正在尝试(但失败)在组ggplot
中使用scale_fill_gradient()。我正在使用以下数据和ggplot
代码。
我正在尝试获得与以下内容相似的东西,但对于每个组(颜色不必只是尝试缩放颜色的渐变而相同)
ggplot(xy, aes(x=Year, y=Gain, group=Gain, label=Variable)) +
geom_bar(stat="identity", color="black", fill="#9C27B0", position="dodge") +
geom_text(position=position_dodge(width=0.9), hjust=-0.05) +
ggtitle("Feature Importance (All years)") +
guides(fill=FALSE) +
scale_x_discrete(limits=unique(xy$Year)) +
coord_flip() +
theme_bw(base_size = 11, base_family = "") +
scale_fill_gradient2(low="red", mid="yellow", high="green")
数据:
xy <- structure(list(Variable = c("CF.NCL", "DailySALES.EBIT", "EBIT.FinExp",
"EQ.Turnover", "SALES.WC", "CA.CL", "CA.TA", "TL.EQ", "CL.FinExp",
"TL.TA", "logTA", "TL.EQ", "DailySALES.EBIT", "CF.NCL", "CA.CL",
"SALES.WC", "EQ.Turnover", "CL.FinExp", "CA.TA", "EBIT.FinExp",
"TL.TA", "logTA", "SALES.WC", "DailySALES.EBIT", "TL.EQ", "CA.CL",
"CF.NCL", "CA.TA", "CL.FinExp", "EQ.Turnover", "EBIT.FinExp",
"TL.TA", "logTA", "SALES.WC", "TL.EQ", "EQ.Turnover", "CA.TA",
"CA.CL", "CL.FinExp", "CF.NCL", "DailySALES.EBIT", "logTA", "TL.TA",
"EBIT.FinExp"), Gain = c(0.0249669149256489, 0.0284788358072123,
0.0416885482543631, 0.0481161711992678, 0.0484199735868597, 0.0494398516158408,
0.0771009818151621, 0.0835709725586987, 0.0922406378720892, 0.120398717131196,
0.385578395233662, 0.0188783661019333, 0.0197853002129162, 0.0272749207536935,
0.0347284667673124, 0.0430992866122089, 0.0435834750808189, 0.0631609275004249,
0.0641364686807323, 0.0787809479913172, 0.212397184938682, 0.39417465535996,
0.0210381458789509, 0.0279003966700287, 0.0321333141168294, 0.0337989653595418,
0.0450490096266459, 0.0491798397528832, 0.0661536580122029, 0.0734706661210229,
0.135767436486281, 0.167947601913985, 0.347560966061628, 0.0139475947749436,
0.0186058318290004, 0.0223217808071512, 0.0244533394205631, 0.0257086851408409,
0.0410344050892873, 0.0530577543373713, 0.119669956742959, 0.153321518346671,
0.169880049476648, 0.357999084034564), Year = c("FourthYear",
"FourthYear", "FourthYear", "FourthYear", "FourthYear", "FourthYear",
"FourthYear", "FourthYear", "FourthYear", "FourthYear", "FourthYear",
"ThirdYear", "ThirdYear", "ThirdYear", "ThirdYear", "ThirdYear",
"ThirdYear", "ThirdYear", "ThirdYear", "ThirdYear", "ThirdYear",
"ThirdYear", "SecondYear", "SecondYear", "SecondYear", "SecondYear",
"SecondYear", "SecondYear", "SecondYear", "SecondYear", "SecondYear",
"SecondYear", "SecondYear", "FirstYear", "FirstYear", "FirstYear",
"FirstYear", "FirstYear", "FirstYear", "FirstYear", "FirstYear",
"FirstYear", "FirstYear", "FirstYear")), row.names = c(NA, -44L
), vars = "Year", drop = TRUE, indices = list(33:43, 0:10, 22:32,
11:21), group_sizes = c(11L, 11L, 11L, 11L), biggest_group_size = 11L, .Names = c("Variable",
"Gain", "Year"), labels = structure(list(Year = c("FirstYear",
"FourthYear", "SecondYear", "ThirdYear")), class = "data.frame", row.names = c(NA,
-4L), vars = "Year", drop = TRUE, indices = list(c(0L, 1L, 5L,
6L, 8L, 15L, 24L, 33L, 36L, 38L, 41L), c(7L, 11L, 16L, 20L, 21L,
23L, 29L, 31L, 32L, 34L, 42L), c(4L, 10L, 12L, 13L, 19L, 22L,
27L, 28L, 35L, 37L, 40L), c(2L, 3L, 9L, 14L, 17L, 18L, 25L, 26L,
30L, 39L, 43L)), group_sizes = c(11L, 11L, 11L, 11L), biggest_group_size = 11L, .Names = "Year"), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
当前情节:
我想要达到的目标类似于: (但我有不同的数据)
答案 0 :(得分:1)
也许这可以帮助您:
colrs=c()
for (i in unique(xy$Year)){
gainx=xy$Gain[xy$Year==i]
colrs=c(colrs,colorRampPalette(c("darkblue", "lightblue" ))(length(gainx))[rank(gainx)])
}
ggplot(xy, aes(x=Year, y=Gain, group=Gain, label=Variable)) +
geom_bar(stat="identity", color="black", fill=colrs, position="dodge") +
geom_text(position=position_dodge(width=0.9), hjust=-0.05) +
ggtitle("Feature Importance (All years)") +
guides(fill=FALSE) +
scale_x_discrete(limits=unique(xy$Year)) +
coord_flip() +
theme_bw(base_size = 11, base_family = "") +
scale_fill_gradient2(low="red", mid="yellow", high="green")