r - Plot number of observations of bars in a histogram using ggplot -
I want to add the number of comments for each variable above a histogram's bar, here's my dataframe
mean & lt; - Structure (List (Driver = C ( "crop agriculture", "crop farming", "Infrastructure", "Infrastructure", "mining", "mining", "mixed farm", "mixed farm", "other land use" , "Other land use", "grassland", "grassland", "tree crops", "tree crops", "water", "water"), period = c ("1990-2000", "2000-2005" "1990-2000", "2000-2005", "1990-2000", "2000-2005", "1990-2000", "2000-2005", "1990-2000", "2000-2005", "1990 2000 "," 2000 to 2005 "," 1990 to 2000 "," 2000 to 2005 "," 1990 to 2000 "," 2000 to 2005 "), mean = c (36.2697273704497, 61.3311804191792, 28.7523209391483, 30.9 55,220,240,622, 49.4900570536558 , 41.5037095947389, 13.1454310847706, 10.3642833385884, 28.587199 6967629, 23.3988064930454, 53.9768942854543, 61.3460606144189, 17.3260123546446, 16.1278503954073, 36.6165841628849, 32.6896740558384), N = c ("N = 1669", "N = 783", "N = 9 2", "N = 1623", "N = 151" "," N = 151 "," n = 20 "," n = 7 "," n = 1355 "," n = 925 "," n = 851 "," N = 10986 "," n = 6039 " "N" = 316 "," n = 211 "," n = 466 "," N = 244 ").), Name = c (" driver "," period "," mean "," n "), class = "Data.frame", line.Name = C (NA, -16L)) Based on this dataframe, this is what I want to plot.
Meaning barplot & lt; - ggplot (instrument, AES (X = driver, y = mean, across = Length, width = .85)) + geom_bar (status = "dodge", status = "Identity") + Labs (x = "", y = "EF (T / O)") + theme (axis. text = Eliment_tekst (size = 16), Aksh- tithle = element_text (size = 20) legend. caption = Element_text (size = 20 face = "bold") , Lijendktekst = Eliment_tekst (size = 20), Akshklain = Element_line (color = "black")) + Scale_fill_grey ( "period") + Scale_y_continuous (range = C (0, 70)) + subject category (Adhar_ Size = 20, base_family = "") + theme (panel.grid.m However, I want to add to this plot, for each variable the observation of the memorandum (in this case, for each period Driver for). I have not succeeded in doing this so far. Can somebody help me out? Thank you.
One way is ggplot_build () and annotate < / Code>. Once you create a ggplot object, you want to get values for x and y positions. Here, the object is g when you use the ggplot_build (objectname) $ data [[1]] , you can get the required value annotated ( ) In , you use the x and y values in the foo and the label you want (i.e., mydf $ n). As an additional, I added the topic (axis.text.x = element_text (angle = 45, hjust = 1)) to modify the x code label.
g < - Jigplot (Maindf, AES (X = driver, Y = means the = Length, width = 0.85)) + Jiom_bar (status = "dodge", State = "Identity") + Labs (X = "", y = "EF (T / is)") + Theme (axis.text = element_text (size = 16), axis-tithle = element_text (size = 20), legendattel = element_text (size = 20, face = "bold"), legend .text = element_text (size = 20), axis.line = element_line (color = "black")) + scale_fill_grey ("period") + scale_y_continuous (range = c (070)) + theme_services (base_size = 20, base_family = "") + Theme (panel.grid.minor = element_line (color = "brown", size = 0.5)) + theme (axis.text .x = element_text (angle = 45, hjust = 1)) ### Annotate () foo & lt; - ggplot_build (g) $ data [[1]] G + annotate ("text", x = foo $ x, y = foo $ y + 1, create a data frame for label = mydf $ N, size = 3)
Comments
Post a Comment