geom_bar()给出一个空图 [英] geom_bar() giving an empty plot
问题描述
我对 ggplot2
包中的 geom_bar()
函数有问题.我不知道为什么会得到空的情节!当我过滤数据集中的 Variable =='DDIFF_IK'
时,未绘制该图,其他任何 Variable
都可以正常工作.我希望任何人都可以给我一些提示,以解决数据方面的问题.
I have a problem with geom_bar()
function from ggplot2
package. I do not have an idea why i do get an empty plot! When i filter my dataset that Variable == 'DDIFF_IK'
, the plot is not drawn, any other Variable
is working well. I hope anyone can give me a tip what is wrong with the data or so.
这里仅是 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
的示例数据集:
Here is sample dataset with other 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()
...
is invalid in my case here is the sample of the bigger dataset (data3
), which is working well and ggplot()
is drawn...
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))
推荐答案
问题不是您显示的大小,而是数据中存在 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,]
的图形,则会绘制该图.
The problem is not the size, as you showed, but the fact that in data there is data$Value_y[6]
and data$Value_y[5]
are both -0.15. This leads to overlapping.
Curiously data$Value_y[6]==data$Value_y[5]
returns FALSE
so I didn't catch it when comparing length(unique(data$Value_y)
with nrow(data)
.
But if you do remove either one with data=data[-6,]
or data=data[-5,]
, it will draw the plot.
这篇关于geom_bar()给出一个空图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!