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.
Related
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)
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")
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),"%"))
This question already has answers here:
How to change legend title in ggplot
(14 answers)
Closed 4 years ago.
I used R and ggplot to do a small-multiple graph.
ggplot(data=datatest,aes(x=Percentage,y=Accuracy,group=interaction(Classifiers, Feature), color=interaction(Classifiers, Feature)))+geom_line()+facet_grid(OS ~ Dataset)
The graph I got is:
How can I remove change the legend, for example, I want to change interaction(Classifiers,Feature) to just 'Approaches', and also, how to change like SVM.Ngram, LG.WE, SVM.WE to just 'approach1','approach2', and 'approach3'.
Try
tbl <- c(
SVM.Ngram = "approach1",
LG.WE = "approach2",
SVM.WE = "approach3"
)
ggplot(data=datatest,
aes(x=Percentage,y=Accuracy,group=interaction(Classifiers, Feature), color=interaction(Classifiers, Feature))) +
geom_line() +
labs(color = "Approaches") +
facet_grid(OS ~ Dataset, labeller = labeller(tbl)
)
This is from http://ggplot2.tidyverse.org/reference/labeller.html - if you check there, it gives more options you might be interested in.
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)