R, ggplot: Decimals on y-axis [duplicate] - r

This question already has answers here:
How do I change the number of decimal places on axis labels in ggplot2?
(4 answers)
Closed 4 years ago.
I want to produce a bar plot, similar to this MWE:
library(tidyverse)
library(ggplot2)
mtcars %>%
mutate(mpg=mpg/1000) %>%
ggplot(aes(x=cyl, y=mpg)) +
geom_bar(stat="identity") +
scale_y_continuous(labels = scales::percent)
What I get is the following (keep in mind that it is nonsense, but serves illustration purposes):
Now, I want the decimals replaced from the percentages on the y-axis ("30%" instead of "30.0%"). What can I do?
I have found a similar question here, but couldn't make the function NRPercent does not work (and can't comment there).

With the new version of scales you can use:
scale_y_continuous(labels = scales::percent_format(accuracy = 1))

Here is a post that would help out : How do I change the number of decimal places on axis labels in ggplot2?
I posted the solution here just so you have it here. Added percent sign to values.
mtcars %>%
mutate(mpg=mpg/1000) %>%
ggplot(aes(x=cyl, y=mpg*100)) +
geom_bar(stat="identity") +
scale_y_continuous("Percent", labels = function(x) paste0(sprintf("%.0f", x),"%"))

Related

Wrapping geom_text / geom_label on ggplot (R) [duplicate]

This question already has answers here:
wrapping long geom_text labels
(2 answers)
Closed 1 year ago.
I am trying to find a way to wrap geom_text/geom_label on a scatterplot (ggplot). I have seen ways this can be done manually inputting the co-ordinates - but i will have 10-20 variables, so this will not be possible.
I have a data frame that looks like this...
df <- data.frame(x =c(2,4,6,8),
y =c(7,3,5,4),
label =c("this variable has a long name which needs to be shortened",
"this variable has an even longer name that really needs to be shortened",
"this variables has a name that is much longer than two of the other name, so really needs to be shortened",
"this is pretty long too"))
and i want to make the following plot (but with wrapped labels)
ggplot(df, aes(x=x, y=y, label=label))+
geom_point()+
geom_text(nudge_y=0.05)+
xlim(0,10)+
theme_minimal()+
ggtitle("title")
This is the plot:
Have you tried str_wrap ? You might need to play around with width argument and nudge_y according to your actual data.
library(ggplot2)
df$label <- stringr::str_wrap(df$label, 25)
ggplot(df, aes(x=x, y=y, label=label))+
geom_point()+
geom_text(nudge_y=0.5)+
xlim(0,10)+
theme_minimal()+
ggtitle("title")

ggplot2: adding labels to geom_bar() for a count [duplicate]

This question already has answers here:
Show frequencies along with barplot in ggplot2
(5 answers)
Closed 8 years ago.
I want to plot frequency distribution of an [r] factor variable as a bargraph, where bars represent the frequency counts of the factor levels. I use ggplot2 to do that and there's no problem with that.
What I can't figure out is how to add frequency count labels to the bars in the bargraph. The syntax that I've tried is as follows:
ggplot(data, aes(x = factorvar)) + geom_bar(fill = "somecolor") + geom_text(aes(y = ???))
I think I thoroughly searched in stackoverflow and "R Graphics Cookbook" by W.Chang but I couldn't find any specific answer to what parameter should I match to "y" in the aesthetics of geom_text() above. I tried some variants like: (y = ..count..) but it didn't work.
I would appreciate any help. Thanks...
ggplot(data=diamonds, aes(x=clarity)) +
geom_bar() +
geom_text(stat='count', aes(label=..count..), vjust=-1)

How to add legends in ggplot2? [duplicate]

This question already has answers here:
Add legend to ggplot2 line plot
(4 answers)
Closed 4 years ago.
Probably very basic question about the legends in ggplot2 (sorry i am basic user user of R), I use this:
p<-ggplot(bAfr_topS1, aes(MAF, V3))+ geom_point()
p <- p+ geom_point(data=bEur_topS1,aes(MAF,V3),colour="red")+
geom_point(data = bSas_topS1, aes(MAF, V3), colour="blue")
print(p)
but can't see the legends in output plot, any suggestion please? what should i add in?
It's difficult to give specific help without any data, but to illustrate the point #PoGibas is making and to get you started, try the following
library(tidyverse)
bind_rows(list(AF = bAfr_topS1, EU = bEur_topS1, PK = bSas_topS1), .id = "src") %>%
ggplot(aes(MAF, V3, colour = as.factor(src))) +
geom_point()
This assumes that bAfr_topS1, bEur_topS1, bSas_topS1 all have the same column structure.

How to add data labels [duplicate]

This question already has answers here:
Show frequencies along with barplot in ggplot2
(5 answers)
Closed 8 years ago.
I want to plot frequency distribution of an [r] factor variable as a bargraph, where bars represent the frequency counts of the factor levels. I use ggplot2 to do that and there's no problem with that.
What I can't figure out is how to add frequency count labels to the bars in the bargraph. The syntax that I've tried is as follows:
ggplot(data, aes(x = factorvar)) + geom_bar(fill = "somecolor") + geom_text(aes(y = ???))
I think I thoroughly searched in stackoverflow and "R Graphics Cookbook" by W.Chang but I couldn't find any specific answer to what parameter should I match to "y" in the aesthetics of geom_text() above. I tried some variants like: (y = ..count..) but it didn't work.
I would appreciate any help. Thanks...
ggplot(data=diamonds, aes(x=clarity)) +
geom_bar() +
geom_text(stat='count', aes(label=..count..), vjust=-1)

Add customized labels over bars in ggplot [duplicate]

This question already has an answer here:
How to add custom labels from a dataset on top of bars using ggplot/geom_bar in R?
(1 answer)
Closed 6 years ago.
I am plotting a simple barplot in ggplot2 and I need to show, over each bar of the plot, something (a number, or a string..) which is not related to this dataset I'm using.
For example with the following instructions:
ggplot(diamonds,aes(cut))+geom_bar()
I get this graph:
And I want to show, over the bars, the elements of the array:
val<-c(10,20,30,40,50)
Obtaining a result like this other graph
I tried using geom_text in this way:
ggplot(diamonds,aes(cut))+geom_bar()+
geom_text(aes(label=val))
But I get the following error message
Error: Aesthetics must be either length 1 or the same as the data (53940): label, x
The problem is that you are making a histogram with geom_bar and there is no y variable specified. In order to apply this example, you need to summarise the cut variable first:
val<-c(10,20,30,40,50)
library(dplyr)
diamonds %>%
group_by(cut) %>%
tally() %>%
ggplot(., aes(x = cut, y = n)) +
geom_bar(stat = "identity") +
geom_text(aes(label = val), vjust = -0.5, position = position_dodge(0.9))
which gives you:

Resources