geom-text

Positioning values of stacked barchart in the center using ggplot2 [duplicate]

巧了我就是萌 提交于 2019-12-02 14:32:43
问题 This question already has answers here : Showing data values on stacked bar chart in ggplot2 (2 answers) Closed 2 years ago . I've created a stacked barchart and faceted using two variables. I'm unable to position the values in the barchart in the middle, it either appears at the extremes or overlaps. The expected image is the following: I've pasted the script below. Any help would be appreciated. library(dplyr) library(reshape2) library(ggplot2) year<-c("2000","2000","2010","2010","2000",

How to add a complex label with italics and a variable to ggplot?

好久不见. 提交于 2019-11-30 21:26:58
I have read many postings on this topic using expression() , paste() , and bquote() , or some combination. I think I am close to solving my problem, but I just can't get there. The following script generates a plot labelled with "y = 1 + 2(x); r^2= 0.9". How can I italicize "y" and "x", and italicize the "r" and superscript the 2 of "r^2"? If I have overlooked a relevant earlier post, sorry, but please direct me to it. df <- data.frame(x=c(1:5), y=c(1:5)) a <- 1 b <- 2 r2 <- 0.9 eq <- paste("y = ", a, " + ", b, "(x); r^2=", r2) ggplot(data=df, aes(x=x, y=y))+ geom_point(color="black")+ geom

Alternate geom_text position with hjust

自闭症网瘾萝莉.ら 提交于 2019-11-30 09:07:15
I'm plotting a stacked bar graph and use geom_text to insert the value of each stack. The difficulty I'm facing is that some stacks are very small/narrow, so that the text of two stacks overlap each other and hence is not very readable. I would like to adjust the text positioning in a way that for example the text position alternates between hjust == 1 and hjust == -1 for each stack, so that there will be no overlaps (or any other method that will result in readable text). Here's an example of what I'm currently doing (a dput of mydf is provided below): library(ggplot2) ggplot(mydf, aes(x

ggplot2: geom_text resize with the plot and force/fit text within geom_bar

£可爱£侵袭症+ 提交于 2019-11-30 06:46:27
This is actually two questions in one (not sure if goes against SO rules, but anyway). First question is how can I force a geom_text to fit within a geom_bar ? (dynamically according to the values plotted) Looking around, the solutions I found were changing the size of the label. That certainly works, but not for every case. You can change the size for a specific plot to make the text fit within the bar, but when the data changes, you may need to manually change the size of the text again. My real-life problem is that I need to generate the same plot for constantly changing data (daily), so I

Alternate geom_text position with hjust

佐手、 提交于 2019-11-29 14:24:04
问题 I'm plotting a stacked bar graph and use geom_text to insert the value of each stack. The difficulty I'm facing is that some stacks are very small/narrow, so that the text of two stacks overlap each other and hence is not very readable. I would like to adjust the text positioning in a way that for example the text position alternates between hjust == 1 and hjust == -1 for each stack, so that there will be no overlaps (or any other method that will result in readable text). Here's an example

Change geom_text's default “a” legend to label string itself

老子叫甜甜 提交于 2019-11-29 01:32:46
Similarly to this question , I want to change the default "a" in the legend, but rather than removing it completely, I want to replace it with the labels themselves. That is, the first line of the legend should have a colored icon labeled "se" with the full name "setosa" on the right. iris$abbrev = substr( iris$Species, 1, 2 ) ggplot(data = iris, aes(x = Sepal.Length, y=Sepal.Width, shape = Species, colour = Species)) + geom_text(aes(label = abbrev)) You can change the legend key generating function. This still requires a bit of manual intervention, but arguably less than using the grobs.

annotate boxplot in ggplot2

青春壹個敷衍的年華 提交于 2019-11-28 11:27:37
I've created a side-by-side boxplot using ggplot2. p <- ggplot(mtcars, aes(x=factor(cyl), y=mpg)) p + geom_boxplot(aes(fill=factor(cyl))) I want to annotate with min, max, 1st quartile, median and 3rd quartile in the plot. I know geom_text() can do so and may be fivenum() is useful. But I cannot figure out how exactly I can do!. These values should be displayed in my plot. The most succinct way I can think of is to use stat_summary . I've also mapped the labels to a color aesthetic, but you can, of course, set the labels to a single color if you wish: ggplot(mtcars, aes(x=factor(cyl), y=mpg,

Show the value of a geom_point using geom_text

本秂侑毒 提交于 2019-11-28 11:03:07
问题 Using this nice code from here: countries <- structure(list(country = structure(c(5L, 6L, 3L, 4L, 10L, 8L, 11L, 7L, 1L, 13L, 9L, 12L, 2L), .Label = c("Australia", "China", "France", "Georgia", "India", "Ireland", "Malaysia", "Poland", "Qatar", "Singapore", "South Africa", "Spain", "USA"), class = "factor"), Latitude = c(20.593684, 53.142367, 46.227638, 32.165622, 1.352083, 51.919438, -30.559482, 4.210484, -25.274398, 37.09024, 25.354826, 40.463667, 35.86166), Longitude = c(78.96288, -7.692054

Change geom_text's default “a” legend to label string itself

我的梦境 提交于 2019-11-27 16:02:16
问题 Similarly to this question, I want to change the default "a" in the legend, but rather than removing it completely, I want to replace it with the labels themselves. That is, the first line of the legend should have a colored icon labeled "se" with the full name "setosa" on the right. iris$abbrev = substr( iris$Species, 1, 2 ) ggplot(data = iris, aes(x = Sepal.Length, y=Sepal.Width, shape = Species, colour = Species)) + geom_text(aes(label = abbrev)) 回答1: You can change the legend key

R: ggplot stacked bar chart with counts on y axis but percentage as label

六月ゝ 毕业季﹏ 提交于 2019-11-27 14:31:35
I'm looking for a way to label a stacked bar chart with percentages while the y-axis shows the original count (using ggplot). Here is a MWE for the plot without labels: library(ggplot2) df <- as.data.frame(matrix(nrow = 7, ncol= 3, data = c("ID1", "ID2", "ID3", "ID4", "ID5", "ID6", "ID7", "north", "north", "north", "north", "south", "south", "south", "A", "B", "B", "C", "A", "A", "C"), byrow = FALSE)) colnames(df) <- c("ID", "region", "species") p <- ggplot(df, aes(x = region, fill = species)) p + geom_bar() I have a much larger table and R counts quite nicely the different species for every