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")
Related
This question already has an answer here:
Limit ggplot2 axes without removing data (outside limits): zoom
(1 answer)
Closed 1 year ago.
I have an issue with my grouped barplots, I want to visualize gene expression, and since some genes had very high expression I decided to se the ylim to different values to better visualize the lower values. However the plots come out looking like this, where the bar marked with 'y' is cut off misleading in the second picture where the limit is set to 5000. Any ideas to why this is and potenitally how to fix it? Ive used this code
grouped_bars <-ggplot(data, aes(x=gene_name, y=Result, fill=Day))+geom_bar(stat="identity",colour="black", position="dodge")+ scale_fill_manual(values =c("blue","red")) + ylim(0, 5000)
Use coord_cartesian() instead of ylim() to avoid your bars from getting lost.
grouped_bars <- ggplot(data, aes(x=gene_name, y=Result, fill=Day)) +
geom_bar(stat="identity",colour="black", position="dodge") +
scale_fill_manual(values =c("blue","red")) +
coord_cartesian(ylim=c(0,5000))
grouped_bars
This question already has an answer here:
difference between the two ways of using aes in ggplot?
(1 answer)
Closed 3 years ago.
When looking at examples of making basic plots using ggplot2, I have noticed that some examples provide the aesthetic mappings for their x and y axes in the original ggplot() function. On the other hand, other examples only provide the data in the ggplot() call, and provide the aesthetic mappings for x and y in whatever geom_ argument they use. Both examples create the same plots. Is there a functional difference that matters? Is this a matter of preference, or are there appropriate times for each?
data(mpg)
ggplot(mpg, aes(x=displ, y=hwy))+
geom_point()
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy))
ggplot uses a hierachical system of layers to provide detailled control over the plots.
Consider the following two cases:
Here we specify the element color in the geom. Therefore it is only going to be used by geom_point() but not by geom_line() (the lines are black)
ggplot(mtcars, aes(mpg, hp)) +
geom_point(aes(color=cyl)) +
geom_line()
In the second example the element color is specified in the ggplot() call. It is going to be used by all subsequent layers (geom_point() and geom_line() use it)
ggplot(mtcars, aes(mpg, hp, color=cyl)) +
geom_point() +
geom_line()
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.
This question already has answers here:
Easily add an '(all)' facet to facet_wrap in ggplot2?
(3 answers)
Closed 5 years ago.
It is often the case that we produce facets to decompose the data according to a variable, but that we still would like to see a summary as a stack of the facets. Here is an example:
library(ggplot2)
ggplot(data=iris, aes(x=Sepal.Length,y=Petal.Length)) +
geom_point(aes(color=Species)) +
facet_wrap(~Species, ncol=2)
However, I would also like that one of the facets is the overlay of the 3 facets:
ggplot(data=iris, aes(x=Sepal.Length,y=Petal.Length)) +
geom_point(aes(color=Species))
Is there anyway of doing this easily?
Many thanks,
I wrote the following function to duplicate the dataset and create an extra copy under of the data under variable all.
library(ggplot2)
# Create an additional set of data
CreateAllFacet <- function(df, col){
df$facet <- df[[col]]
temp <- df
temp$facet <- "all"
return(rbind(temp, df))
}
Instead of overwriting the original facet data column, the function creates a new column called facet. The benefit of this is that we can use the original column to specify the aesthetics of the plot point.
df <- CreateAllFacet(iris, "Species")
ggplot(data=df, aes(x=Sepal.Length,y=Petal.Length)) +
geom_point(aes(color=Species)) +
facet_wrap(~facet, ncol=2)
I feel the legend is optional in this case, as it largely duplicates information already available within the plot. It can easily be hidden with the extra line + theme(legend.position = "none")
Using ggplot2 I have made facetted histograms using the following code.
library(ggplot2)
library(plyr)
df1 <- data.frame(monthNo = rep(month.abb[1:5],20),
classifier = c(rep("a",50),rep("b",50)),
values = c(seq(1,10,length.out=50),seq(11,20,length.out=50))
)
means <- ddply (df1,
c(.(monthNo),.(classifier)),
summarize,
Mean=mean(values)
)
ggplot(df1,
aes(x=values, colour=as.factor(classifier))) +
geom_histogram() +
facet_wrap(~monthNo,ncol=1) +
geom_vline(data=means, aes(xintercept=Mean, colour=as.factor(classifier)),
linetype="dashed", size=1)
The vertical line showing means per month is to stay.
But I want to also add text over these vertical lines displaying the mean values for each month. These means are from the 'means' data frame.
I have looked at geom_text and I can add text to plots. But it appears my circumstance is a little different and not so easy. It's a lot simpler to add text in some cases where you just add values of the plotted data points. But cases like this when you want to add the mean and not the value of the histograms I just can't find the solution.
Please help. Thanks.
Having noted the possible duplicate (another answer of mine), the solution here might not be as (initially/intuitively) obvious. You can do what you need if you split the geom_text call into two (for each classifier):
ggplot(df1, aes(x=values, fill=as.factor(classifier))) +
geom_histogram() +
facet_wrap(~monthNo, ncol=1) +
geom_vline(data=means, aes(xintercept=Mean, colour=as.factor(classifier)),
linetype="dashed", size=1) +
geom_text(y=0.5, aes(x=Mean, label=Mean),
data=means[means$classifier=="a",]) +
geom_text(y=0.5, aes(x=Mean, label=Mean),
data=means[means$classifier=="b",])
I'm assuming you can format the numbers to the appropriate precision and place them on the y-axis where you need to with this code.