1

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.

Here is the sample data only for 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))

and 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))

Thanks in advance!

[Addition]

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))

above code to create plot is working...how come?

[Addition 2]

Just to prove that the answer:

If you use data=data[1:5,] you'll get a drawn plot. If you take a larger subset it simply can't be drawn properly

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))
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Mal_a
  • 3,670
  • 1
  • 27
  • 60
  • If you're using `geom_bar` you will want your x_axis discrete rather than continuous. – Haboryme Feb 02 '17 at 09:21
  • You have to use `Value_y` as `factor()` in `aes`: `aes(x=factor(Value_y), y=prop, fill=Variables)`. – ottlngr Feb 02 '17 at 09:22
  • Thanks a lot, i have not thought about that... however it wonders me why the data2 (in **Addition**) is working fine with continuous axis (its actually another sample of my data)...any ideas? – Mal_a Feb 02 '17 at 09:31
  • If you use `data=data[1:5,]` you'll get a drawn plot. If you take a larger subset it simply can't be drawn properly. – Haboryme Feb 02 '17 at 09:41
  • Well Your point @Haboryme is invalid, have a look at **Addition 2** in my question, bigger dataset `data3`, and still the plot is drawn.. – Mal_a Feb 02 '17 at 09:50

1 Answers1

2

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.

Haboryme
  • 4,611
  • 2
  • 18
  • 21