2017-02-02 2 views
0

ggplot2 패키지의 geom_bar() 기능에 문제가 있습니다. 나는 아이디어가 없다 왜 내가 빈 줄거리를 얻을! Variable == 'DDIFF_IK' 인 데이터 집합을 필터링 할 때 플롯이 그려지지 않으며 다른 어떤 Variable도 잘 작동합니다. 나는 누군가가 저에게 정보가 잘못된 점을 알려주기를 바랍니다. 여기 geom_bar() 빈 줄을 제공합니다

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

감사합니다! 노력하고 플롯을 만들 수있는 코드 위

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

... 어떻게 온 : 여기

[추가]

다른 Variable 샘플 데이터 세트입니까?

[추가 2]

그냥 대답을 증명합니다 :

당신이 데이터 = 데이터를 사용하는 경우

[1 : 5,] 당신이 그려 음모를 얻을 수 있습니다. 더 큰 집합을 경우 단순히 ...

여기 내 경우에는 무효 잘 ggplot() 그려 노력하고 더 큰 데이터 세트 (data3)의 샘플입니다 제대로 그릴 수 없습니다

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

만약 당신이'geom_bar'를 사용한다면, x_axis가 연속적이지 않고 분리되어 있기를 원할 것입니다. – Haboryme

+0

''aes' :'aes' (x = factor (Value_y), y = prop, fill = Variables)'에서'Value_y'를'factor()'로 써야합니다. – ottlngr

+0

고맙습니다. 그러나 그것에 대해 생각하지 않았습니다 ... 그러나 그것은 왜 data2 (** 추가 **)가 연속 축 (실제로 내 데이터의 다른 샘플)과 잘 작동하는지 궁금합니다 ... 어떤 아이디어입니까? –

답변

1

문제는 크기가 아니라 앞에서 설명한 것처럼 데이터에 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,] 중 하나를 제거하면 플롯이 그려집니다.

+0

나는 실제로 그것에 대한 해결책을 찾을 수 없습니다, 그리고 여기 그것은! 고마워요! –

관련 문제