geom_bar()
包中的ggplot2
函数存在问题。我没有一个想法为什么我会得到一个空的情节!当我过滤我的Variable == 'DDIFF_IK'
数据集时,未绘制绘图,任何其他Variable
都运行良好。我希望任何人都可以给我一个提示数据左右的错误。
以下是仅Variable == 'DDIFF_IK'
的示例数据:
data <- structure(list(Value_y = c(-0.189999999999998, -0.179999999999993,
-0.170000000000002, -0.159999999999997, -0.150000000000006, -0.149999999999991,
-0.140000000000001, -0.129999999999995, -0.120000000000005, -0.109999999999999,
-0.0999999999999943, -0.0900000000000034, -0.0799999999999983,
-0.0699999999999932, -0.0600000000000023, -0.0499999999999972,
-0.039999999999992, -0.0300000000000011, -0.019999999999996,
-0.0100000000000051, 0.0100000000000051, 0.019999999999996, 0.0300000000000011,
0.0400000000000063, 0.0499999999999972, 0.0600000000000023, 0.0700000000000074,
0.0799999999999983, 0.0900000000000034, 0.100000000000009, 0.109999999999999,
0.120000000000005, 0.129999999999995, 0.140000000000001, 0.150000000000006,
0.159999999999997), Variables = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "WW_DD_IK", class = "factor"),
n = c(1L, 1L, 5L, 11L, 13L, 6L, 21L, 29L, 38L, 25L, 15L,
11L, 19L, 15L, 28L, 22L, 19L, 13L, 3L, 4L, 2L, 7L, 9L, 6L,
6L, 2L, 12L, 13L, 21L, 23L, 28L, 13L, 15L, 14L, 5L, 1L),
prop = c(0.00210084033613445, 0.00210084033613445, 0.0105042016806723,
0.023109243697479, 0.0273109243697479, 0.0126050420168067,
0.0441176470588235, 0.0609243697478992, 0.0798319327731092,
0.0525210084033613, 0.0315126050420168, 0.023109243697479,
0.0399159663865546, 0.0315126050420168, 0.0588235294117647,
0.046218487394958, 0.0399159663865546, 0.0273109243697479,
0.00630252100840336, 0.00840336134453781, 0.00420168067226891,
0.0147058823529412, 0.0189075630252101, 0.0126050420168067,
0.0126050420168067, 0.00420168067226891, 0.0252100840336134,
0.0273109243697479, 0.0441176470588235, 0.0483193277310924,
0.0588235294117647, 0.0273109243697479, 0.0315126050420168,
0.0294117647058824, 0.0105042016806723, 0.00210084033613445
)), class = "data.frame", .Names = c("Value_y", "Variables",
"n", "prop"), row.names = c(NA, -36L))
和ggplot()
:
library(ggplot2)
library(scales)
ggplot(data=data, aes(x=Value_y, y=prop, fill=Variables)) +
geom_bar(stat="identity", position = "dodge") +
scale_x_continuous(breaks = pretty_breaks(n = 10)) + ylab("n / sum(n)") +
theme(plot.title = element_text(size=20, face="bold"),
axis.text.y=element_text(size=15),
axis.text.x=element_text(size=15),
axis.title.x = element_text(size=18, face="bold"),
axis.title.y = element_text(size=18, face="bold"),
legend.position="bottom", legend.title=element_blank(),
legend.text=element_text(size=14))
提前致谢!
[加法]
以下是包含其他Variable
的示例数据集:
data2 <- structure(list(Value_y = c(-0.4, -0.39, -0.38, -0.37, -0.36,
-0.35, -0.34, -0.33), Variables = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "WW_DD_IK", class = "factor"), n = c(3L,
11L, 32L, 47L, 22L, 15L, 14L, 3L), prop = c(0.0204081632653061,
0.0748299319727891, 0.217687074829932, 0.319727891156463, 0.149659863945578,
0.102040816326531, 0.0952380952380952, 0.0204081632653061)), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("Value_y", "Variables", "n",
"prop"), row.names = c(NA, -8L))
以上代码创建情节正在运作......怎么来的?
[添加2]
只是为了证明答案:
如果您使用data = data [1:5,],您将获得绘制的图表。如果您选择较大的子集,则无法正确绘制
在我的情况下无效,这里是更大的数据集(data3
)的样本,它运行良好并且绘制了ggplot()
...
data3 <- structure(list(Value_y = c(-0.19, -0.17, -0.14, -0.12, -0.11,
-0.1, -0.09, -0.08, -0.07, -0.06, -0.05, -0.04, -0.03, -0.02,
-0.01, 0, 0.01, 0.02, 0.03, 0.04, 0.180000000000007, 0.189999999999998,
0.200000000000003, 0.210000000000008, 0.219999999999999, 0.230000000000004,
0.239999999999995, 0.25, 0.260000000000005, 0.269999999999996,
0.280000000000001, 0.290000000000006, 0.299999999999997, 0.310000000000002,
0.320000000000007, 0.329999999999998, 0.340000000000003, 0.350000000000009,
0.359999999999999, 0.409999999999997, 0.420000000000002, 0.430000000000007,
0.439999999999998, 0.450000000000003, 0.460000000000008, 0.469999999999999,
0.480000000000004, 0.489999999999995, 0.5, 0.510000000000005,
0.519999999999996, 0.530000000000001), Variables = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("DD_ABM", "DDIFF_AK"), class = "factor"),
n = c(1L, 3L, 1L, 4L, 15L, 18L, 19L, 21L, 38L, 40L, 57L,
48L, 46L, 32L, 49L, 36L, 25L, 15L, 7L, 1L, 1L, 4L, 9L, 22L,
10L, 21L, 36L, 36L, 42L, 37L, 25L, 17L, 6L, 5L, 4L, 11L,
12L, 2L, 1L, 1L, 10L, 16L, 9L, 19L, 24L, 22L, 18L, 14L, 13L,
20L, 7L, 2L), prop = c(0.00210084033613445, 0.00630252100840336,
0.00210084033613445, 0.00840336134453781, 0.0315126050420168,
0.0378151260504202, 0.0399159663865546, 0.0441176470588235,
0.0798319327731092, 0.0840336134453782, 0.119747899159664,
0.100840336134454, 0.0966386554621849, 0.0672268907563025,
0.102941176470588, 0.0756302521008403, 0.0525210084033613,
0.0315126050420168, 0.0147058823529412, 0.00210084033613445,
0.00210084033613445, 0.00840336134453781, 0.0189075630252101,
0.046218487394958, 0.0210084033613445, 0.0441176470588235,
0.0756302521008403, 0.0756302521008403, 0.0882352941176471,
0.0777310924369748, 0.0525210084033613, 0.0357142857142857,
0.0126050420168067, 0.0105042016806723, 0.00840336134453781,
0.023109243697479, 0.0252100840336134, 0.00420168067226891,
0.00210084033613445, 0.00210084033613445, 0.0210084033613445,
0.0336134453781513, 0.0189075630252101, 0.0399159663865546,
0.0504201680672269, 0.046218487394958, 0.0378151260504202,
0.0294117647058824, 0.0273109243697479, 0.0420168067226891,
0.0147058823529412, 0.00420168067226891)), class = "data.frame", .Names = c("Value_y",
"Variables", "n", "prop"), row.names = c(NA, -52L))
答案 0 :(得分:2)
问题不在于大小,正如您所示,但数据中data$Value_y[6]
和data$Value_y[5]
都是-0.15。这导致重叠
好奇地data$Value_y[6]==data$Value_y[5]
会返回FALSE
,所以在将length(unique(data$Value_y)
与nrow(data)
进行比较时,我没有抓住它。
但是,如果您删除了data=data[-6,]
或data=data[-5,]
,则会绘制该图。