我一直在[这里](geom_bar + geom_line: with different y-axis scale?)
关注对类似问题的答案。我有一个数据框:
structure(list(ReleaseYear = c(2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016), ReleaseMonth = c("JAN", "JAN", "JAN",
"JAN", "JAN", "FEB", "FEB", "MARCH", "MARCH", "MARCH", "MARCH",
"MARCH", "MARCH", "MARCH", "APRIL", "APRIL", "APRIL", "APRIL",
"APRIL", "MAY"), ProductionOrder = c(10024497, 10025083, 10025082,
10025207, 10024619, 10024597, 10025071, 10024495, 10024948, 10024949,
10025560, 10025583, 10025561, 10025604, 10025741, 10025799, 10025798,
10025904, 10025836, 10025661), NilesHeatNo = c("8G44683", "8K44915",
"8K44931", "8F45082", "8G45098", "8K45265", "8K45308", "8H45420",
"8G45530", "8F45528", "8G45573", "8F45630", "8G45615", "8H45757",
"8H45911", "8J45988", "8F46002", "9G46088", "8V46078", "8K46185"
), LookupCode = c("C1 - OXYGEN - HIGH", "C2 - OXYGEN - LOW",
"C2 - OXYGEN - LOW", "C1 - OXYGEN - HIGH", "C1 - OXYGEN - HIGH",
"C1 - OXYGEN - HIGH", "C1 - OXYGEN - HIGH", "C1 - OXYGEN - HIGH",
"C1 - OXYGEN - HIGH", "C1 - OXYGEN - HIGH", "C1 - OXYGEN - HIGH",
"C1 - OXYGEN - HIGH", "C1 - OXYGEN - HIGH", "C1 - OXYGEN - HIGH",
"C1 - OXYGEN - HIGH", "C1 - OXYGEN - HIGH", "C1 - OXYGEN - HIGH",
"C1 - OXYGEN - HIGH", "C1 - OXYGEN - HIGH", "C1 - OXYGEN - HIGH"
), ScrapWeight = c(0, 1900, 1900, 4700, 1900, 1700, 1700, 0,
0, 0, 0, 20289, 11200, 1900, 1900, 19777, 20843, 2500, 1600,
1700), Melt = c("8G", "8K", "8K", "8F", "8G", "8K", "8K", "8H",
"8G", "8F", "8G", "8F", "8G", "8H", "8H", "8J", "8F", "9G", "8V",
"8K"), MeltNo = c(44683, 44915, 44931, 45082, 45098, 45265, 45308,
45420, 45530, 45528, 45573, 45630, 45615, 45757, 45911, 45988,
46002, 46088, 46078, 46185), NilesWeight = c(20062, NA, NA, 20452,
20692, NA, NA, 20885, 13490, 13546, 20500, 20289, 20150, 20695,
20681, 19777, 20843, NA, NA, NA), CantonWeight = c(NA, 20618,
19947, NA, NA, 20363, 20290, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 18551, 20150, 20398), Grade = c("766GV", "566JV", "566JV",
"766BV", "766FU", "666GV", "666JV", "766FV", "766IV", "766IV",
"764KV", "766BV", "764KV", "766BV", "766BV", "766BV", "766BV",
"565SV", "566BV", "666FL"), MeltDate = structure(c(16770, 16788,
16789, 16808, 16810, 16825, 16828, 16839, 16848, 16848, 16852,
16857, 16861, 16869, 16884, 16892, 16893, 16901, 16904, 16910
), class = "Date"), MeltControlRelease = structure(c(1452902400,
1453075200, 1453248000, 1454976000, 1453766400, 1455148800, 1456185600,
1456099200, 1457481600, 1457481600, 1457395200, 1458259200, 1458691200,
1459123200, 1460419200, 1461024000, 1461024000, 1461283200, 1461715200,
1462838400), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
O2Range = c("16/190", NA, NA, "16/20", "16/20", NA, NA, "16/19",
"17/20", "17/20", "14/18", "16/20", "14/18", "17/20", "16/20",
"16/20", "16/20", NA, NA, NA), ScrapPct = c("50", NA, NA,
"50", "0", NA, NA, "50(T)", "50(T)", "50(T)", "70", "70(T)",
"70(T)", "50(T)", "50(T)", "70", "70", NA, NA, NA), ReasonLate = c(NA,
"LOW O2 TOP, CROP 2900", "Low O2 btm, crop 1900", "HIGH O2 BOTTOM, CROP 4700 LBS",
"HIGH O2 BOTTOM, CROP 1900", "HIGH O2 BOTTOM, CROP 1700 LBS",
"HIGH O2 BTM", "HIGH O2 BOTTOM, CROP 1900", "HIGH O2 CTR",
"HIGH O2 CTR", "HIGH O2 TOP AND CENTER", "HIGH O2, FORGE TO SCRAP",
"HIGH O2 TOP, CROP 11200", "USED MONICO SCRAP, HIGH O2 BTM",
"HIGH O2 BTM, CROP 1900", "High O2, forge to scrap (sister to 8F46002)",
"High O2, forge to scrap (sister to 8J45988)", "HIGH O2 BTM",
"HIGH O2 BOTTOM, CROP 20150", "HIGH o2 BOTTOM, CROP 1700 LBS"
), O2High = list("190", NA_character_, NA_character_, "20",
"20", NA_character_, NA_character_, "19", "20", "20",
"18", "20", "18", "20", "20", "20", "20", NA_character_,
NA_character_, NA_character_), O2Low = list("16", NA_character_,
NA_character_, "16", "16", NA_character_, NA_character_,
"16", "17", "17", "14", "16", "14", "17", "16", "16",
"16", NA_character_, NA_character_, NA_character_), Turnings = c(0,
NA, NA, 0, 0, NA, NA, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, NA, NA,
NA), Furnace = c("G", "K", "K", "F", "G", "K", "K", "H",
"G", "F", "G", "F", "G", "H", "H", "J", "F", "G", "V", "K"
), DailyAverageRelativeHumidity = c(91L, 65L, 53L, 70L, 81L,
52L, 67L, 75L, 81L, 81L, 71L, 62L, 68L, 56L, 57L, 76L, 73L,
68L, 58L, 47L), DailyPrecipitation = structure(c(13L, 151L,
2L, 29L, 39L, 151L, 151L, 7L, 5L, 5L, 2L, 151L, 25L, 8L,
45L, 19L, 21L, 5L, 2L, 2L), .Label = c("", "0.00", "0.01",
"0.02", "0.03", "0.04", "0.05", "0.06", "0.07", "0.08", "0.09",
"0.10", "0.11", "0.12", "0.13", "0.14", "0.15", "0.16", "0.17",
"0.18", "0.19", "0.20", "0.21", "0.22", "0.23", "0.24", "0.25",
"0.26", "0.27", "0.28", "0.29", "0.30", "0.31", "0.32", "0.33",
"0.34", "0.35", "0.36", "0.37", "0.38", "0.39", "0.40", "0.41",
"0.42", "0.43", "0.44", "0.45", "0.46", "0.47", "0.48", "0.49",
"0.50", "0.51", "0.52", "0.53", "0.54", "0.55", "0.56", "0.57",
"0.58", "0.59", "0.60", "0.61", "0.62", "0.63", "0.64", "0.65",
"0.66", "0.67", "0.68", "0.69", "0.70", "0.71", "0.72", "0.73",
"0.74", "0.75", "0.76", "0.77", "0.78", "0.79", "0.80", "0.81",
"0.82", "0.83", "0.84", "0.85", "0.86", "0.87", "0.88", "0.89",
"0.90", "0.91", "0.92", "0.93", "0.94", "0.95", "0.96", "0.99",
"1.00", "1.02", "1.04", "1.05", "1.06", "1.07", "1.08", "1.10",
"1.11", "1.12", "1.18", "1.19", "1.20", "1.21", "1.22", "1.23",
"1.24", "1.25", "1.27", "1.28", "1.30", "1.32", "1.35", "1.37",
"1.41", "1.42", "1.44", "1.46", "1.47", "1.49", "1.52", "1.54",
"1.58", "1.62", "1.68", "1.69", "1.71", "1.74", "1.84", "1.86",
"1.95", "1.96", "1.97", "2.06", "2.08", "2.11", "2.21", "2.67",
"3.50", "3.96", "3.99", "T"), class = "factor"), DailySnowfall = structure(c(2L,
63L, 2L, 2L, 10L, 2L, 63L, 4L, 5L, 5L, 2L, 63L, 63L, 2L,
2L, 2L, 15L, 3L, 2L, 2L), .Label = c("", "0.0", "0.1", "0.2",
"0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "1.0", "1.1",
"1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "10.7",
"2.0", "2.1", "2.2", "2.3", "2.4", "2.5", "2.6", "2.7", "2.8",
"2.9", "3.0", "3.1", "3.2", "3.3", "3.4", "3.5", "3.6", "3.7",
"3.8", "3.9", "4.0", "4.1", "4.2", "4.3", "4.4", "4.6", "4.7",
"4.8", "5.1", "5.2", "5.4", "5.5", "5.9", "6.4", "6.6", "6.7",
"6.8", "7.2", "7.5", "7.9", "T"), class = "factor"), DailyWeather = structure(c(80L,
159L, 1L, 80L, 147L, 73L, 159L, 147L, 161L, 161L, 1L, 159L,
127L, 74L, 74L, 136L, 147L, 145L, 1L, 1L), .Label = c("",
"BR", "BR FG", "BR HZ", "BR HZ FG", "BR RA", "BR TS FG",
"BR TS RA", "DU", "DZ", "DZ BR", "DZ FG BR", "DZ FG MIFG BR",
"DZ FZDZ FG BR", "DZ FZDZ SN BR", "DZ FZDZ SN PL FG BR RA",
"DZ FZRA FZDZ SN BR RA", "DZ FZRA FZDZ SN PL BR RA", "DZ MIFG BR FG",
"DZ MIFG BR HZ FG", "DZ SN BR", "DZ SN BR RA", "FC TS RA BR",
"FG", "FG BR", "FG BR HZ", "FG FZFG BR", "FG FZFG BR HZ",
"FG FZFG MIFG BR", "FG MIFG", "FG MIFG BR", "FG MIFG BR HZ",
"FZDZ BR", "FZDZ FG FZFG BR", "FZDZ SN", "FZDZ SN BR", "FZDZ SN BR BLSN RA",
"FZDZ SN BR RA", "FZDZ SN BR RA FG", "FZDZ SN FG FZFG BR",
"FZDZ SN FZFG BR FG", "FZFG BR FG", "FZFG MIFG BR FG", "FZRA",
"FZRA FZDZ SN BR", "FZRA FZDZ SN BR HZ", "FZRA FZDZ SN BR RA",
"FZRA FZDZ SN PL BR", "FZRA FZDZ SN PL BR BLSN", "FZRA FZDZ SN PL BR RA",
"FZRA FZDZ SN PL BR UP", "FZRA FZDZ SN PL FG FZFG BR RA",
"FZRA PL BR", "FZRA SN BR", "FZRA SN BR BLSN", "FZRA SN BR RA",
"FZRA SN PL", "FZRA SN PL BR", "FZRA SN PL BR RA", "FZRA SN RA",
"HZ", "MIFG BR", "MIFG BR FG", "MIFG BR HZ", "MIFG BR HZ FG",
"MIFG BR RA FG", "MIFG BR TS FG", "MIFG BR TS RA FG", "MIFG FG",
"MIFG FG HZ", "MIFG RA FG", "MIFG TS FG", "RA", "RA BR",
"RA BR DZ", "RA BR FG", "RA BR HZ", "RA BR TS", "RA DZ",
"RA DZ BR", "RA DZ BR HZ", "RA DZ FG BR", "RA DZ FG MIFG BR",
"RA DZ FG MIFG BR TS", "RA DZ FZDZ FG BR", "RA DZ FZDZ FZFG BR FG",
"RA DZ FZDZ SN BR", "RA DZ FZDZ SN FG FZFG BR", "RA DZ FZRA FZDZ BR",
"RA DZ FZRA FZDZ SN BR", "RA DZ FZRA FZDZ SN FG BR", "RA DZ FZRA FZDZ SN PL BR",
"RA DZ FZRA FZDZ SN PL BR UP", "RA DZ FZRA FZDZ SN PL FG BR",
"RA DZ FZRA FZDZ SN PL FZFG BR", "RA DZ FZRA SN BR", "RA DZ FZRA SN FG BR",
"RA DZ FZRA SN FZFG BR FG", "RA DZ FZRA SN PL FG BR", "RA DZ FZRA SN PL FG MIFG BR",
"RA DZ FZRA SN PL FZFG BR FG", "RA DZ MIFG BR FG", "RA DZ MIFG FG",
"RA DZ PL BR", "RA DZ SN", "RA DZ SN BR", "RA DZ SN FG BR",
"RA DZ SN PL BR", "RA DZ SN PL FG BR", "RA FG BR", "RA FG BR HZ",
"RA FG BR TS", "RA FG FZFG MIFG BR", "RA FG MIFG", "RA FG MIFG BR",
"RA FG MIFG BR HZ", "RA FG MIFG BR TS", "RA FZDZ SN", "RA FZDZ SN BR",
"RA FZDZ SN BR BLSN", "RA FZDZ SN FG BR HZ BLSN", "RA FZDZ SN FG FZFG BR",
"RA FZRA BR", "RA FZRA FZDZ BR", "RA FZRA FZDZ SN BR", "RA FZRA PL BR",
"RA FZRA SN", "RA FZRA SN BR", "RA FZRA SN FZFG BR FG", "RA FZRA SN PL BR",
"RA FZRA SN PL BR BLSN", "RA FZRA SN PL FG BR", "RA FZRA SN PL FZFG BR BLSN FG",
"RA HZ", "RA MIFG BR", "RA MIFG BR FG", "RA MIFG BR HZ",
"RA MIFG BR HZ FG", "RA MIFG BR TS FG", "RA MIFG DZ FG",
"RA MIFG FG", "RA MIFG TS FG", "RA PL", "RA PL BR", "RA SN",
"RA SN BLSN", "RA SN BR", "RA SN BR BLSN", "RA SN BR HZ",
"RA SN FG BR", "RA SN FG FZFG BR", "RA SN FZFG BR FG", "RA SN MIFG BR FG",
"RA SN PL", "RA SN PL BR", "RA SN PL FG BR", "RA SN PL FG MIFG BR",
"RA TS", "SN", "SN BLSN", "SN BR", "SN BR BLSN", "SN BR FG",
"SN BR HZ", "SN FG", "SN FG BR", "SN FG BR BLSN", "SN FG FZFG BR",
"SN FG FZFG BR BLSN", "SN FZFG BLSN FG", "SN FZFG BR", "SN FZFG BR BLSN FG",
"SN FZFG BR FG", "SN FZFG BR UP FG", "SN FZFG FG", "SN MIFG BR FG",
"SN PL", "SN PL BR", "SN PL BR HZ", "SN PL FZFG BR BLSN FG",
"SN PL FZFG BR FG", "TS", "TS BR", "TS FG MIFG BR", "TS FG MIFG RA BR",
"TS FZDZ SN PL BR BLSN RA", "TS GR RA", "TS GR RA BR", "TS GR RA FG BR",
"TS MIFG BR FG", "TS MIFG BR HZ FG", "TS MIFG BR RA FG",
"TS MIFG FG", "TS RA", "TS RA BR", "TS RA BR DZ", "TS RA BR FG",
"TS RA BR HZ", "TS RA DZ BR", "TS RA DZ FG BR", "TS RA DZ FG MIFG BR",
"TS RA DZ MIFG BR HZ FG", "TS RA DZ PL BR", "TS RA FG", "TS RA FG BR",
"TS RA FG BR HZ", "TS RA FG MIFG", "TS RA FG MIFG BR", "TS RA FG MIFG BR HZ",
"TS RA MIFG BR", "TS RA MIFG BR FG", "TS RA MIFG BR HZ FG",
"TS RA MIFG FG", "TS RA MIFG HZ FG", "TS RA SN BR", "TS RA SN PL BR",
"TS SN PL BR"), class = "factor")), row.names = c(NA, -20L
), class = c("tbl_df", "tbl", "data.frame"))
我试图创建一个条形图,显示从2016年到2018年每个月的事件总数,并有一个重叠的线图显示了每年每个月的废料重量总和。我的ggplot调用是:
ggplot(O2Data %>% filter(year(MeltDate) != 2019), aes(x=factor(months(MeltDate), levels=c("January","February","March","April","May","June","July","August",
"September","October","November","December")))) +
geom_bar(stat="count", aes(fill=months(MeltDate))) +
geom_line(aes(y=ScrapWeight, group=factor(year(MeltDate)), color=factor(year(MeltDate)))) +
stat_summary(aes(y=ScrapWeight/1000), fun.y=sum) +
scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Scrap Weight")) +
theme(axis.text.x=element_text(angle=90, hjust=1, vjust=0.5), legend.position="none") +
labs(title = "Count of O2 Scrap Codes by Month (2016-2018)", x = "Month", y = "Count")
我得到的最终情节看起来根本不像我想要的那样,但无法完全弄清我的错误。
我可以绘制前半部分,但是对geom_line()的调用似乎覆盖了它。关于如何使折线图按我的预期工作的任何想法?
ggplot(O2Data %>% filter(year(MeltDate) != 2019), aes(x=factor(months(MeltDate), levels=c("January","February","March","April","May","June","July","August",
"September","October","November","December")))) +
geom_bar(stat="count", aes(fill=months(MeltDate))) +
# geom_line(aes(y=ScrapWeight, group=factor(year(MeltDate)), color=factor(year(MeltDate)))) +
# stat_summary(aes(y=ScrapWeight/1000), fun.y=sum) +
scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Scrap Weight")) +
theme(axis.text.x=element_text(angle=90, hjust=1, vjust=0.5), legend.position="none") +
labs(title = "Count of O2 Scrap Codes by Month (2016-2018)", x = "Month", y = "Count")
答案 0 :(得分:0)
由于上面的Axeman评论,我发现了我的错误,该错误与缩放有关。
此外,我不正确地调用geom_line()和stat_summary()。
更正的代码:
ggplot(O2Data %>% filter(year(MeltDate) != 2019), aes(x=factor(months(MeltDate), levels=c("January","February","March","April","May","June","July","August",
"September","October","November","December")))) +
geom_bar(stat="count", fill="antiquewhite3") +
geom_line(stat='summary', aes(y=ScrapWeight/100, group=factor(year(MeltDate)), color=factor(year(MeltDate)))) +
stat_summary(aes(y=ScrapWeight/100), fun.y=sum) +
scale_y_continuous(sec.axis = sec_axis(~.*100, name = "Scrap Weight"), limits = c(0, 48)) +
theme(axis.text.x=element_text(angle=90, hjust=1, vjust=0.5), legend.position="none") +
labs(title = "Count of O2 Scrap Codes by Month (2016-2018)", x = "Month", y = "Count")