ggplot密度图:每组不同的x轴

时间:2018-11-29 16:37:22

标签: r ggplot2

我正在尝试绘制一些数据的多个密度图。

我有以下代码:

ggplot(data=stack) +
  geom_density(aes(x=OfferPrice, group=Country, fill=Country), 
               alpha=0.5, adjust=2) +
  facet_grid(~Country) +
  theme_bw()

我的问题出现在每个密度图的x轴上。

更仔细地查看数据:

stack %>%
  group_by(Country) %>%
  summarise(min(OfferPrice),
            mean(OfferPrice),
            max(OfferPrice))

看起来Country-JN的数字很高。早些时候,我削减了每组极限值的最高和最低5%,因此不一定要有极限值。我认为错误的是,图中的x轴使用了JN组中的最大值。为何每个组都有不同的x轴?

数据:

stack <- structure(list(Country = c("US", "GB", "US", "HK", "JN", "US", 
"CH", "CA", "US", "US", "CA", "JN", "GB", "AU", "US", "GB", "US", 
"GB", "HK", "CH", "CA", "CA", "US", "GB", "TA", "JN", "CA", "CA", 
"CA", "CA", "CH", "GB", "CA", "HK", "CA", "US", "US", "CA", "US", 
"AU", "CA", "CA", "CA", "US", "GB", "GB", "AU", "US", "US", "AU", 
"CA", "CA", "CA", "US", "CA", "GB", "CA", "US", "GB", "US", "AU", 
"AU", "US", "CA", "US", "GB", "AU", "CH", "CA", "CA", "GB", "AU", 
"AU", "CH", "CA", "AU", "CH", "US", "CH", "TA", "AU", "AU", "GB", 
"CH", "HK", "AU", "AU", "CA", "US", "TA", "GB", "US", "AU", "US", 
"CA", "CA", "US", "AU", "CA", "US", "CA", "US", "CA", "US", "CA", 
"US", "US", "AU", "CA", "AU", "GB", "US", "HK", "AU", "US", "CA", 
"JN", "JN", "GB", "JN", "CA", "CA", "AU", "GB", "GB", "US", "US", 
"US", "AU", "GB", "CA", "CA", "US", "CH", "GB", "US", "US", "AU", 
"GB", "CH", "JN", "CA", "AU", "CA", "US", "US", "AU", "AU", "CA", 
"US", "GB", "GB", "US", "US", "CA", "US", "HK", "AU", "US", "GB", 
"US", "GB", "GB", "US", "CA", "JN", "CA", "AU", "CA", "CA", "GB", 
"CA", "HK", "HK", "US", "CH", "US", "AU", "TA", "US", "CH", "HK", 
"AU", "US", "HK", "GB", "AU", "CH", "US", "AU", "US", "CH", "US", 
"CH", "CA", "AU", "HK", "CA", "US", "CH", "GB", "CA", "CA", "CA", 
"CA", "US", "CA", "CA", "US", "HK", "US", "HK", "AU", "GB", "AU", 
"CH", "US", "AU", "CA", "CA", "US", "GB", "AU", "US", "CH", "CA", 
"CA", "CA", "US", "AU", "GB", "GB", "CA", "AU", "CA", "AU", "US", 
"HK", "AU", "US", "AU", "CA", "US", "US", "US", "CA", "GB", "CA", 
"US", "CA", "US", "AU", "CA", "US", "AU", "CH", "GB", "CA", "CA", 
"CA", "CA", "HK", "AU", "TA", "AU", "GB", "AU", "CA", "JN", "US", 
"CA", "CA", "AU", "US", "US", "GB", "CA", "US", "GB", "US", "CA", 
"CA", "CH", "US", "US", "US", "US", "US", "HK", "CH", "CA", "CA", 
"CA", "AU", "GB", "CH", "CA", "GB", "CA", "AU"), EffectiveDate = structure(c(17617, 
17500, 17556, 17596, 17618, 17667, 17786, 17728, 17569, 17760, 
17585, 17613, 17806, 17847, 17786, 17665, 17702, 17683, 17574, 
17725, 17723, 17658, 17563, 17847, 17584, 17578, 17842, 17522, 
17697, 17521, 17680, 17794, 17813, 17697, 17773, 17578, 17556, 
17595, 17669, 17501, 17655, 17562, 17668, 17589, 17582, 17780, 
17812, 17667, 17611, 17758, 17751, 17617, 17505, 17505, 17725, 
17506, 17513, 17541, 17644, 17702, 17828, 17688, 17696, 17519, 
17850, 17746, 17779, 17547, 17806, 17701, 17638, 17759, 17786, 
17770, 17835, 17750, 17841, 17653, 17788, 17553, 17794, 17654, 
17724, 17675, 17802, 17638, 17625, 17514, 17709, 17758, 17681, 
17507, 17708, 17758, 17800, 17569, 17821, 17493, 17834, 17848, 
17639, 17549, 17590, 17575, 17630, 17639, 17760, 17724, 17701, 
17725, 17756, 17794, 17511, 17494, 17822, 17758, 17709, 17492, 
17605, 17709, 17602, 17644, 17814, 17696, 17760, 17603, 17730, 
17675, 17590, 17724, 17743, 17680, 17690, 17711, 17560, 17570, 
17702, 17512, 17626, 17618, 17576, 17731, 17527, 17709, 17729, 
17672, 17515, 17527, 17641, 17597, 17812, 17540, 17602, 17743, 
17662, 17709, 17505, 17737, 17603, 17515, 17568, 17843, 17738, 
17508, 17816, 17578, 17590, 17812, 17743, 17528, 17616, 17687, 
17646, 17515, 17816, 17557, 17507, 17564, 17777, 17802, 17511, 
17842, 17584, 17556, 17547, 17844, 17590, 17674, 17759, 17583, 
17836, 17721, 17724, 17801, 17578, 17808, 17682, 17849, 17708, 
17515, 17746, 17633, 17759, 17591, 17802, 17540, 17560, 17588, 
17800, 17787, 17821, 17724, 17645, 17527, 17722, 17556, 17704, 
17844, 17619, 17792, 17577, 17637, 17843, 17765, 17688, 17562, 
17834, 17738, 17653, 17645, 17718, 17676, 17637, 17570, 17490, 
17534, 17646, 17625, 17766, 17808, 17675, 17786, 17808, 17555, 
17739, 17802, 17617, 17619, 17667, 17634, 17662, 17711, 17806, 
17513, 17627, 17673, 17574, 17647, 17609, 17619, 17521, 17543, 
17686, 17807, 17613, 17543, 17543, 17528, 17694, 17576, 17584, 
17521, 17605, 17618, 17723, 17641, 17683, 17823, 17634, 17844, 
17836, 17816, 17539, 17583, 17618, 17687, 17589, 17602, 17717, 
17535, 17718, 17625, 17822, 17651, 17521, 17751, 17617, 17563, 
17578, 17772), class = "Date"), OfferPrice = c(44, 13, 33, 0.3, 
3000, 23, 6.26, 0.35, 10, 6.25, 0.25, 7110, 109.5, 0.11, 16, 
2, 5.5, 15, 0.5, 8.5, 0.5, 0.2, 5, 92.5, 22, 103740, 0.23, 0.75, 
8.65, 1.23, 17.4, 1.5, 0.38, 0.84, 8.1, 27.5, 10, 1, 14, 0.42, 
0.1, 1.82, 2, 39.8, 238, 340, 0.3, 4.5, 41.5, 0.2, 0.25, 0.27, 
0.35, 5, 0.3, 115, 0.15, 5, 142.25, 14, 2.43, 0.02, 24, 0.115, 
8.25, 25, 0.155, 9.6, 0.67, 6, 52.5, 0.2, 0.2, 1.37, 1.6, 0.65, 
2.9, 4, 7, 72, 0.025, 0.14, 22.5, 6.75, 0.64, 0.8, 0.8, 0.4, 
22, 94.8, 15, 10, 2.45, 34, 1, 9.3, 6.25, 0.018, 0.2, 24.5, 0.3, 
2.9, 0.35, 2.05, 0.4, 29.5, 2.26, 0.36, 0.75, 0.027, 2.8, 16, 
3.54, 0.018, 10, 0.15, 1780, 1602, 120, 3900, 0.25, 0.18, 2.32, 
269, 175, 18, 18, 23, 0.2, 10, 0.1, 0.6, 4.8, 6, 164.5, 7, 26.42, 
0.02, 190, 11, 992, 0.2, 1.42, 0.55, 23, 33.5, 0.35, 0.065, 1.16, 
29.5, 65, 8, 27.5, 18.9, 0.15, 17, 0.63, 0.34, 26.25, 0.65, 6.9, 
10, 6.75, 1.21, 0.95, 73125, 2.5961, 0.054, 1.2, 9.64, 251, 2.46, 
0.18, 0.375, 9.97, 20.43, 25.5, 0.025, 60, 3, 1.55, 0.5, 0.2, 
17, 0.443, 8, 0.05, 5.25, 1.15, 0.45, 7.155, 17, 24.5, 12.5, 
2.1, 0.75, 0.35, 0.39, 38.2, 0.63, 16, 0.15, 0.1, 0.12, 2.32, 
10, 0.3, 1.66, 17, 0.4, 1.3, 0.3, 1.08, 30, 0.8, 10.88, 0.9, 
0.21, 0.17, 1.7, 2.25, 1, 0.08, 1.5, 14.75, 0.35, 0.44, 0.35, 
17, 2, 37, 195, 0.165, 0.02, 0.2, 0.015, 25, 1.09, 0.45, 10, 
0.145, 0.92, 36, 13.25, 4, 0.6, 101, 0.7, 15, 0.3, 0.7, 0.06, 
0.25, 6.5, 1.1, 16.72, 1.25, 0.1, 0.12, 0.28, 1.18, 0.4, 0.02, 
75, 0.08, 5, 0.02, 0.5, 2878, 8, 0.15, 0.33, 0.1, 21.25, 1, 12, 
5.83, 4.25, 1.65, 3.15, 3, 0.15, 7.4, 31.25, 12, 24, 19.75, 41.5, 
0.88, 13.65, 0.25, 0.15, 0.25, 2.35, 101, 1.26, 1.65, 10, 1.32, 
0.5)), row.names = c(NA, -300L), class = c("tbl_df", "tbl", "data.frame"
))

1 个答案:

答案 0 :(得分:2)

您可以在set wsCSV = ... (your csv worksheet) set rng = wsCSV.UsedRange set ws = ... (your worksheet) ws.UsedRange.Offset(1).ClearContents() ws.Range("A2").Resize(rng.Rows.Count, rng.Columns.Count).Values = rng.Values 中指定是否要使用自由标度(x,y或两者)。

facet_grid

enter image description here