Visualize a portion of bar plot using ggplot()

rstudio

#1

Hello,

I have a data set of about 500 products, and I have done some computations on them to produce a profit margin percentage. I want to focus on the products that fall into a specific range of profit percentage. To be specific, I want to focus on the products with more than 20% profit percentage.

My goal to product a barplot of those products and ignore the rest.
Can I achieve that? I can imagine that the x-axis would be the names of the products, the y-axis would be the percentage and the height of the column would be the profit margin percentage with the percentage on the top of each column.
Or I do not even care for the y axis since it will take a lot of space. All I care is the names of the products on the x-axis and the height of the column represents the percentage.

Also, some columns have kinda long names (not as long as a sentence) so I am sure their names will run over another product's name on the x-axis. I have seen some plots that have the variables name kinda slighted on the x-axis like this:


Can I do the same thing here?

library(tidyverse) 
library(scales) 
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor
library(cowplot) 
#> 
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#> 
#>     ggsave
library(dplyr)
library(ggplot2)
library(ff)
#> Loading required package: bit
#> Attaching package bit
#> package:bit (c) 2008-2012 Jens Oehlschlaegel (GPL-2)
#> creators: bit bitwhich
#> coercion: as.logical as.integer as.bit as.bitwhich which
#> operator: ! & | xor != ==
#> querying: print length any all min max range sum summary
#> bit access: length<- [ [<- [[ [[<-
#> for more help type ?bit
#> 
#> Attaching package: 'bit'
#> The following object is masked from 'package:base':
#> 
#>     xor
#> Attaching package ff
#> - getOption("fftempdir")=="/var/folders/kw/rtv7qm_j67l_kf2n71cvxm5m0000gn/T//RtmpazRKnL"
#> - getOption("ffextension")=="ff"
#> - getOption("ffdrop")==TRUE
#> - getOption("fffinonexit")==TRUE
#> - getOption("ffpagesize")==65536
#> - getOption("ffcaching")=="mmnoflush"  -- consider "ffeachflush" if your system stalls on large writes
#> - getOption("ffbatchbytes")==16777216 -- consider a different value for tuning your system
#> - getOption("ffmaxbytes")==536870912 -- consider a different value for tuning your system
#> 
#> Attaching package: 'ff'
#> The following objects are masked from 'package:bit':
#> 
#>     clone, clone.default, clone.list
#> The following objects are masked from 'package:utils':
#> 
#>     write.csv, write.csv2
#> The following objects are masked from 'package:base':
#> 
#>     is.factor, is.ordered
library(plotrix)
#> 
#> Attaching package: 'plotrix'
#> The following object is masked from 'package:scales':
#> 
#>     rescale
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following object is masked from 'package:bit':
#> 
#>     setattr
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
library(reprex)

sample_long = structure(list(ITEM_CATEGORY_KEY = c(160L, 175L, 176L, 177L, 
                                              178L, 179L, 260L, 5996L, 11010L, 11011L, 11012L, 11015L, 11020L, 
                                              11030L, 11099L, 13006L, 13007L, 13008L, 13009L, 13010L, 13015L, 
                                              13025L, 13030L, 13040L, 13045L, 13099L, 14010L, 14020L, 14030L, 
                                              15010L, 15020L, 15030L, 15035L, 15040L, 15060L, 15065L, 15070L, 
                                              15080L, 15095L, 15096L, 15099L, 16010L, 16020L, 16030L, 16045L, 
                                              16050L, 16051L, 16055L, 16060L, 16099L, 17010L, 17020L, 17030L, 
                                              17045L, 17050L, 17060L, 17065L, 17070L, 17080L, 17099L, 18010L, 
                                              18020L, 18040L, 18050L, 18060L, 18099L, 19010L, 19020L, 19025L, 
                                              19030L, 19035L, 19040L, 19045L, 19060L, 19070L, 19099L, 20020L, 
                                              20099L, 21010L, 21015L, 21020L, 21030L, 21099L, 23010L, 23020L, 
                                              23030L, 23035L, 23099L, 25010L, 25015L, 25020L, 25040L, 25099L, 
                                              26010L, 26015L, 26020L, 26025L, 26026L, 26030L, 26099L, 28010L, 
                                              28020L, 28030L, 28099L, 29005L, 29009L, 29010L, 29015L, 29020L, 
                                              29023L, 29025L, 29030L, 29035L, 29040L, 29041L, 29042L, 29099L, 
                                              30010L, 30020L, 30099L, 31010L, 31015L, 31017L, 31020L, 31030L, 
                                              31040L, 31099L, 32010L, 32011L, 32015L, 32020L, 32025L, 32030L, 
                                              32035L, 32040L, 32045L, 32050L, 32055L, 32080L, 32081L, 32082L, 
                                              32083L, 32084L, 32085L, 32099L, 33010L, 33020L, 33030L, 33035L, 
                                              33040L, 33045L, 33050L, 33099L, 36010L, 36012L, 36015L, 36020L, 
                                              36025L, 36030L, 36035L, 36040L, 36043L, 36045L, 36050L, 36051L, 
                                              36052L, 36053L, 36055L, 36059L, 36060L, 36061L, 36062L, 36063L, 
                                              36064L, 36065L, 36066L, 36067L, 36068L, 36069L, 36070L, 36080L, 
                                              36081L, 36082L, 36083L, 36085L, 36086L, 36087L, 36088L, 36090L, 
                                              36091L, 36095L, 36096L, 36099L, 38005L, 38008L, 38011L, 38017L, 
                                              38021L, 38070L, 38080L, 38085L, 38086L, 38090L, 38095L, 38099L, 
                                              39010L, 39015L, 39020L, 39025L, 40010L, 40020L, 40025L, 40030L, 
                                              40040L, 40050L, 40099L, 42010L, 42015L, 42020L, 42025L, 42030L, 
                                              42035L, 42036L, 42040L, 42045L, 42050L, 42060L, 42099L, 44010L, 
                                              44020L, 44025L, 44028L, 44030L, 44040L, 44099L, 46010L, 46011L, 
                                              46012L, 46015L, 46020L, 46025L, 46030L, 46035L, 46042L, 46045L, 
                                              46046L, 46047L, 46050L, 46055L, 46065L, 46070L, 46080L, 46083L, 
                                              46099L, 48010L, 48015L, 48020L, 48025L, 48040L, 48099L, 50040L, 
                                              50099L, 51010L, 51015L, 51020L, 51030L, 51035L, 52010L, 52020L, 
                                              52025L, 52030L, 52033L, 52035L, 52040L, 52099L, 54010L, 54012L, 
                                              54015L, 54020L, 54025L, 54030L, 54035L, 54099L, 55010L, 55011L, 
                                              55015L, 55016L, 55017L, 55099L, 56010L, 56013L, 56015L, 56020L, 
                                              56025L, 56030L, 56040L, 56045L, 56047L, 56048L, 56050L, 56099L, 
                                              58010L, 58012L, 58015L, 58021L, 58022L, 58023L, 58024L, 58025L, 
                                              58026L, 58027L, 58028L, 58029L, 58030L, 58033L, 58035L, 58040L, 
                                              58045L, 58050L, 58052L, 58060L, 58070L, 58075L, 58083L, 58085L, 
                                              58086L, 58090L, 58095L, 58099L, 60010L, 60012L, 60015L, 60016L, 
                                              60020L, 60025L, 60030L, 60099L, 61005L, 61010L, 61030L, 61040L, 
                                              61099L, 62005L, 62006L, 62009L, 62010L, 62011L, 62012L, 62013L, 
                                              62014L, 62015L, 62016L, 62017L, 62020L, 62025L, 62026L, 62027L, 
                                              62029L, 62030L, 62032L, 62034L, 62035L, 62040L, 62043L, 62044L, 
                                              62050L, 62060L, 62070L, 62071L, 62072L, 62075L, 62080L, 62099L, 
                                              63010L, 63015L, 63020L, 63025L, 63030L, 63035L, 63040L, 63099L, 
                                              64005L, 64006L, 64007L, 64010L, 64015L, 64017L, 64018L, 64019L, 
                                              64020L, 64021L, 64022L, 64025L, 64099L, 66010L, 66015L, 66020L, 
                                              66030L, 66099L, 68010L, 68020L, 68099L, 70021L, 70022L, 70024L, 
                                              70025L, 70026L, 70030L, 70035L, 70037L, 70038L, 70040L, 70045L, 
                                              70099L, 73005L, 73006L, 73007L, 73010L, 73015L, 73020L, 73025L, 
                                              73030L, 73035L, 73040L, 73045L, 73050L, 73060L, 73070L, 73099L, 
                                              74010L, 74015L, 74020L, 74025L, 74030L, 74099L, 76005L, 76010L, 
                                              76012L, 76015L, 76017L, 76020L, 76025L, 76030L, 76035L, 76040L, 
                                              76045L, 76050L, 76055L, 76065L, 76075L, 76080L, 76085L, 76095L, 
                                              76099L, 78010L, 78015L, 78020L, 78099L, 80010L, 80020L, 80030L, 
                                              80099L, 81010L, 81015L, 81020L, 81023L, 81025L, 81030L, 81035L, 
                                              81040L, 81045L, 81050L, 81099L, 83010L, 83011L, 83015L, 83020L, 
                                              83099L, 84010L, 84015L, 84020L, 84025L, 84030L, 84035L, 84050L, 
                                              84099L, 89001L, 89002L, 89003L, 89005L, 89006L, 89007L, 89010L, 
                                              89012L, 89015L, 89020L, 89021L, 89025L, 89030L, 89035L, 89036L, 
                                              89037L, 89038L, 89040L, 89045L, 89060L, 89099L, 90005L, 90015L, 
                                              90045L, 90050L, 90055L, 90070L, 90090L, 92010L, 92099L, 93050L, 
                                              93099L, 98030L, 98080L, 98090L, 98091L, 98097L, 98098L, 98099L, 
                                              99999L), Total_Sales = c(100245.73, 9776828, 8822395.37, 9448123.87, 
                                                                       1615627.52, 4128080.41, 440, -5737.83, 5432257.91, 9745.95, 6397.45, 
                                                                       460015.25, 7096.48, 409.01, 2311415.92, 15101385.08, 1852426.66, 
                                                                       12001170.03, 3368109.05, 1263766.77, 4518497.14, 3156860.69, 
                                                                       861789.26, 1359013.4, 269920.5, 223062.81, 4574493.12, 1499818.09, 
                                                                       383582.44, 50808066.9, 15429325.33, 909918.11, 627502.36, 7510826.75, 
                                                                       35327.37, 123989.84, 4654120.27, 472721.37, 509664.2, 64777.7, 
                                                                       398015.53, 12834375.25, 1839927.94, 13277406.95, 69996.25, 2701373.18, 
                                                                       1165367.77, 3833.53, 1208323.69, 779251.46, 1944387, 13088646.26, 
                                                                       1441837.36, 25459790.55, 668947.05, 5600176.6, 682948.9, 3787695.36, 
                                                                       2287121.19, 443265.23, 30600.28, 4083572.5, 208749.02, 316.5, 
                                                                       100458.76, 483486.15, 14268340.97, 2041127.54, 6952005.26, 2366465.94, 
                                                                       200114.77, 29435.07, 4399.43, 562296.47, 392064.76, 84790.44, 
                                                                       2210.1, 1516.61, 14123337.49, 4138389.02, 291549.08, 26466.64, 
                                                                       10301.21, 1645881.24, 290413.43, 572443.08, 192489.64, 660171.98, 
                                                                       931593.36, 3945885.87, 163575.43, 1403374.46, 257356.59, 483481.91, 
                                                                       111061.89, 91475.6, 865176.03, 1087992.13, 96180.58, 103263, 
                                                                       1262462.1, 6401698.3, 6175.79, 80158.38, 3920242.73, 140128.58, 
                                                                       830093.8, 118097.23, 392139.69, 451848.39, 119503.23, 10218561.11, 
                                                                       3828500.38, 1954278.59, 1168951.46, 71199.77, 244324.7, 1437305.74, 
                                                                       13247267.84, 55161.21, 71194012.42, 10229156.51, 43051.23, 3236255.83, 
                                                                       1520079.33, 9201324.88, 7862550.36, 3838449.28, 3994142.62, 328930.75, 
                                                                       322523.17, 6155304.51, 32476118.56, 19554.76, 134423.47, 8467689.7, 
                                                                       190054.52, 189708.7, 343203.75, 437823.9, 2783811.4, 2526323.1, 
                                                                       1039984.73, 288206.53, 306970.18, 15992.28, 16349.33, 20865.96, 
                                                                       8953.31, 49501.06, 7426.58, 38600.45, 257641.36, 244268.12, 578.29, 
                                                                       132754.06, 165676.78, 320411.41, 42926.62, 316721.29, 837941.77, 
                                                                       694429.42, 1094031.45, 28811.43, 65353.5, 32743.54, 301948.61, 
                                                                       878323.57, 178657.04, 431612.22, 165919.2, 282789.17, 67865.83, 
                                                                       8005.93, 8243397.77, 159942.6, 7001075.71, 270006.45, 1250340.29, 
                                                                       114393.86, 288776.47, 11065.51, 764026.64, 1012516.75, 316411.74, 
                                                                       102777.57, 568130.82, 50224.51, 740878.9, 255422.14, 812513.17, 
                                                                       189278.65, 885877.44, 1594145.23, 27828.84, 4644808.36, 106527.52, 
                                                                       82139.87, 255583.06, 502.15, 152670.44, 2035343.37, 267419.25, 
                                                                       40808.4, 1075559.05, 69176.25, 4642.24, 4197.05, 25063.62, 2042125.16, 
                                                                       602861.48, 164118.72, 351260.25, 29063.1, 143730.52, 139256, 
                                                                       1034685.36, 94314.67, 182765.22, 38922.32, 158464.08, 3712.36, 
                                                                       55792.17, 724143.29, 34281.51, 14654.08, 248930.5, 212761.4, 
                                                                       1078528.5, 2589553.38, 2100019.68, 52963.93, 300596.75, 130871.95, 
                                                                       31665.63, 6147954.6, 2820345.15, 6574.25, 194749.37, 56836.72, 
                                                                       119721.5, 515780.86, 468172.56, 559189.03, 1131878.83, 599223.64, 
                                                                       669481.54, 708533.92, 4565.89, 68325.31, 34684.47, 498311.97, 
                                                                       1189191.71, 1409599.19, 392142.52, 456597.75, 61383.93, 874484.67, 
                                                                       2428058.02, 303926.51, 224.27, 872.35, 604360.22, 6543.8, 1839.42, 
                                                                       43986.91, 9.25, 330962.62, 6184.18, 14162.53, 48461.75, 140564.76, 
                                                                       1747590.52, 280657.71, 69286, 1200832, 1727606.61, 54646.83, 
                                                                       419161.72, 50033.76, 7099385.8, 16566.97, 6087.43, 1062893.14, 
                                                                       1510752.55, 42049.29, 6222.77, 28491.28, 78.05, 5855797.61, 592326.33, 
                                                                       2722387.48, 3204566.15, 1240364.43, 258304.72, 142195.87, 2736773.7, 
                                                                       10379.78, 66673.62, 63909.37, 347378.92, 914098.62, 770609.37, 
                                                                       689145.25, 5232892.13, 52252.56, 3640275.53, 7418435.18, 11789186.27, 
                                                                       972329.82, 3003716.49, 1958072.93, 46959.2, 152679.36, 370976.5, 
                                                                       9803762.23, 1063979.32, 129063.73, 2401657.03, 3373516.66, 106705.88, 
                                                                       1926977.04, 6767555.79, 404326.1, 22573174.57, 675627.47, 12752754.22, 
                                                                       224969.59, 749090.04, 9761.24, 42.1, 4249250.83, 1195822.51, 
                                                                       271065.15, 1057319.17, 90430.36, 72319.94, 15571.78, 254479.48, 
                                                                       178010.13, 541389.07, 46524.07, 69582.71, 80098.24, 3776443.54, 
                                                                       13567501.46, 36400258.85, 222504.94, 1370738.15, 615432.86, 18500908.02, 
                                                                       278520.57, 107373.97, 4167516.71, 5537266.96, 1857732.95, 18456610.02, 
                                                                       222675.81, 11722178.51, 220619.27, 250185.26, 3350714.31, 780267.44, 
                                                                       55584.98, 5309085.23, 52509.15, 7561207.94, 11714940.98, 8940537.39, 
                                                                       1157020.29, 1567680.86, 103875.3, 1492958.96, 51115.02, 55584.64, 
                                                                       41893.31, 1216.81, 1106738.83, 23775.99, 29625.31, 95960.85, 
                                                                       6909505.23, 1135786.76, 202181.67, 1911125.77, 108545.98, 1122863.72, 
                                                                       570008.42, 221129.39, 3734542.08, 245748.02, 1825283.77, 3724651.18, 
                                                                       171292.86, 710251.13, 824181.32, 211167.34, 25893.81, 2851.84, 
                                                                       277058.88, 467349.01, 50010.19, 177746.48, 455846.63, 9800.73, 
                                                                       3725.5, 70393.28, 65902.73, 146765.32, 2258308.41, 858.35, 3825.14, 
                                                                       1936467.68, 610543.43, 919996.33, 6224762.87, 2246303.08, 583094.29, 
                                                                       20586.4, 10589.92, 31377.46, 55011.64, 7657.15, 191604.55, 4255737.05, 
                                                                       1261705.92, 299364.4, 8438.06, 41167.32, 31856.7, 459.92, 54393.86, 
                                                                       42673.01, 1816.88, 35586.57, 3923.75, 3792.03, 34410.59, 303784.55, 
                                                                       9307.16, 1196299.54, 73250.27, 160712.83, 80462.88, 33869.06, 
                                                                       9039.67, 102336.79, 112316.81, 57878.46, 30350.37, 69272.57, 
                                                                       5676.73, 423189.95, 645580.93, 568397.89, 327719.53, 2873245.32, 
                                                                       398871.35, 104672.35, 502578.42, 3312108.57, 59901.68, 325821.94, 
                                                                       50984.1, 159716.59, 5772.31, 27164.22, 28222.55, 26.95, 59827.29, 
                                                                       112231.36, 22107.57, 92194.71, 4280531.68, 2678934.93, 2216395.1, 
                                                                       238994.91, 8278.69, 25131.54, 253250.44, 29682.68, 397791.71, 
                                                                       150709.9, 96225.18, 107728.74, 27231.86, 4266072.89, 1039734.66, 
                                                                       256630.57, 31569.09, 610767.33, 81231.97, 810034.88, 47458.54, 
                                                                       17214069.4, 2145323.53, 1925793.8, 3714.25, 29140.98, 1693.19, 
                                                                       138724.8, 1638269.11, 441290.56, 1111.54, 15652.05, 516.3, 494103.1, 
                                                                       17012.26, 38799.59, 2293.85, 77329.17, 4493308.86, 231610.86, 
                                                                       3176161.79, 1479.5, 4414.42, 56107.35, 18164.14, 270367.7, 2433.84, 
                                                                       249, -665262.22, 372272.36, 5315.98, 187789.07, 1230240.36), 
                        PL_Margins_Percentage = c(0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 
                                                  0, 16, 47, 0, 0, 2, 0, 27, 16, 0, 7, 14, 10, 0, 0, 0, 6, 
                                                  0, 29, 0, 3, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 4, 13, 
                                                  0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 
                                                  1, 5, 19, 6, 0, 0, 0, 0, 7, 3, 0, 0, 0, 3, 4, 0, 0, 0, 9, 
                                                  0, 0, 18, 0, 5, 3, 4, 0, 0, 7, 6, 21, 7, 15, 0, 0, 0, 0, 
                                                  0, 0, 6, 21, 8, 10, 13, 26, 14, 17, 23, 19, 13, 0, 0, 0, 
                                                  2, 0, 0, 0, 0, 0, 0, 0, 0, 9, 7, 0, 0, 0, 2, 0, 0, 23, 0, 
                                                  0, 0, 14, 18, 11, 3, 9, 0, 0, 0, 0, 0, 0, 0, 17, 0, 1, 0, 
                                                  8, 5, 6, 2, 8, 10, 5, 3, 0, 0, 0, 0, 4, 9, 21, 19, 2, 0, 
                                                  0, 1, 0, 0, 0, 0, 21, 0, 0, 11, 0, 0, 19, 13, 0, 2, 2, 7, 
                                                  0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 
                                                  15, 0, 10, 0, 11, 0, 5, 39, 36, 41, 35, 5, 29, 25, 36, 27, 
                                                  0, 6, 0, 0, 0, 0, 0, 0, 0, 15, 1, 0, 9, 5, 28, 19, 1, 1, 
                                                  5, 0, 0, 14, 48, 0, 4, 1, 0, 7, 20, 6, 0, 8, 0, 0, 0, 0, 
                                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 23, 0, 11, 44, 0, 
                                                  27, 35, 0, 8, 2, 0, 0, 0, 0, 34, 8, 43, 33, 28, 0, 0, 0, 
                                                  0, 0, 0, 0, 11, 7, 26, 1, 0, 0, 0, 3, 9, 0, 2, 0, 0, 0, 2, 
                                                  6, 0, 0, 15, 0, 0, 0, 0, 7, 8, 2, 0, 0, 7, 0, 13, 14, 16, 
                                                  13, 8, 0, 0, 0, 7, 0, 0, 24, 16, 9, 17, 13, 0, 13, 0, 13, 
                                                  10, 0, 22, 8, 9, 19, 0, 34, 19, 43, 20, 15, 0, 19, 8, 1, 
                                                  22, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 30, 0, 
                                                  10, 40, 4, 28, 3, 0, 5, 0, 0, 4, 3, 0, 0, 21, 6, 0, 0, 0, 
                                                  0, 0, 0, 4, 3, 2, 29, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 31, 0, 
                                                  13, 1, 5, 1, 0, 0, 3, 0, 20, 17, 0, 6, 0, 24, 5, 2, 7, 0, 
                                                  0, 0, 3, 23, 6, 1, 4, 10, 3, 6, 0, 30, 0, 2, 3, 2, 0, 0, 
                                                  0, 1, 0, 17, 27, 38, 0, 0, 0, 0, 19, 0, 0, 0, 26, 35, 6, 
                                                  3, 0, 47, 25, 34, 10, 24, 26, 22, 1, 3, 3, 0, 45, 37, 33, 
                                                  21, 0, 0, 3, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 13, 0, 0, 0, 
                                                  9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), class = c("tbl_df", 
                                                                                                              "tbl", "data.frame"), row.names = c(NA, -526L))


sample_long_1a = sample_long %>% filter(PL_Margins_Percentage > 0L) %>%
  mutate(category = cut(PL_Margins_Percentage,breaks = c(1,11,21,31,41,50), 
                        labels = c('1%-10%','11%-20%',
                                   '21%-30%','31%-40%','41%-50%'),include.lowest = T,right = F)) 

sample_long_1b <- sample_long_1a %>% group_by(category) %>% summarise(count=n()) %>% 
  mutate(percent= paste0(round(count/sum(count)*100,1),'%')) 

sample_long_2a = 
  sample_long %>% 
  filter (PL_Margins_Percentage > 20L, PL_Margins_Percentage < 31L) 

ggplot(sample_long_2a) + 
  geom_bar(aes(x=ITEM_CATEGORY_KEY,y=PL_Margins_Percentage, fill = ITEM_CATEGORY_KEY),stat='identity') + 
  labs(y='', x='')+ 
  theme(legend.position="none")

Created on 2018-07-06 by the reprex
package
(v0.2.0).


#2

Mara:
I got it.
Thanks!

FYI: I see you are typing^^


#3

This you can achieve through subsetting or using filter(). It seems that you've done this in your reprex, below, but you do so such that it's for greater than 20%, but below 31%.

Also, I'm not sure if this is intentional, but you ultimately plot from a dataset that you've only applied this one filter to (since you build sample_long_2a off of the original sample_long, which means none of what you've done in 1a or 1b is carried along). If that is what you intended to do, a hint for future reprexes would be to cut the material that's not relevant to your plot (i.e. the middle datasets 1a and 1b).

One option you might consider (especially for legibility) is using the coord_flip() function. This is something I started employing a lot after reading Tidy Text Mining, where it's used frequently to look at word counts, etc., since you want to be able to read the text. You can see some examples in the section here:

To do this you can use theme(axis.text.x = element_text(angle = **rotation angle**)) (see more about arguments to element_*() functions).

In the examples below I've used 90° and 60° rotations, respectively.

library(ggplot2)
mtcars2 <- mtcars[1:15, ]
mtcars2$name <- row.names(mtcars2)
ggplot(mtcars2, aes(x = name, y = mpg)) + 
  geom_bar(stat = 'identity', position = "identity") + 
  theme(axis.text.x = element_text(angle = 90, hjust=1))

ggplot(mtcars2, aes(x = name, y = mpg)) + 
  geom_bar(stat = 'identity', position = "identity") + 
  theme(axis.text.x = element_text(angle = 60, hjust=1))

Created on 2018-07-07 by the reprex package (v0.2.0).


#4

Thank you, Mara!
I really appreciate your time!