I am trying to create a simple dotplot that contains 100+ points, some of which are grouped close together. I need the points to be individually labeled, but I would like to stack the labels for points that are close together on top of each other.
Basically, I would like to create a similar dotplot with labeling to the graph below.
As a code example, consider the following code where I would like to add the car name to the dotplot in a way similar to the graphic.
ggplot(mtcars, aes(x = mpg)) +
geom_dotplot(binwidth = .4, stackdir = "centerwhole") +
scale_y_continuous(NULL, breaks = NULL)
Related
I am trying to generate density plot with two overlaid distributions using ggplot2. My data looks like:
diag_elements <- data.frame(x = c(diag(Am.dent), diag(Am.flint)),
group=rep(c("Dent", "Flint"), c(length(diag(Am.dent)), length(diag(Am.flint)))))
And my call to ggplot is:
ggplot(diag_elements) +
geom_density(aes(x=x, colour=group, fill=group), alpha=0.5) +
labs(x = "Diagonal elements of the matrix", y = "Density", fill = "Heterotic Group") +
theme(legend.position = c(0.85, .75))
However, instead of simply renaming the legend with the more complete name specified in fill, this generates a second legend:
Does anyone have any suggestions for getting this same graph, but without the improperly formatted legend?
Thanks!
The other option is guides which allows specific removal of certain legneds. You simply add to your ggplot
+guides(color=FALSE)
I am analyzing the ecological data atm, so I do like to draft a CCA plot that contains information about sites, spp, and environmental variables data. And also coloring geom_text related to the sites and spp separately? Is there any codes enables me to do so in the ggplot2?
I had tried to add two geom_text(spp and sites variables) in the single phrase of codes, but it does not work out as I expected since the geom_text from these two variables are overlapping each other. And then I did try to add colors to geom_text separately, but it does not work out as well. I tried to plot the graph using the auto_plot function of ggvegan, I like the graph the way it orientates the colors and word size of the graph, but the texts were overlapping.
ggplot() +
geom_point(aes(x=CCA1, y=CCA2), data=filter(vare_tbl, ccatype=="species"))+ geom_text_repel(aes(x=CCA1, y=CCA2, label=vgntxt, size=3.5),
data=vare_tbl, seed=123) +
geom_text_repel(aes(x=CCA1, y=CCA2, label=vgntxt, size=3.5),
data=vare_sam_tbl, seed=123)+
geom_segment(aes(x=0, y=0, xend=CCA1, yend=CCA2), arrow=arrow(length = unit(0.2,"cm")),
data=filter(vare_tbl, ccatype=="bp"), color="blue") +
coord_fixed() +
scale_colour_manual(values = c("blue", "black"))+ #this code isn't working
theme_classic() +
theme(legend.position="none")
##Autoplot function of ggvegan
autoplot(cca2, arrows = TRUE, geom = "text", legend = "none")
I obtained the codes to plot the biplot graph from https://blogs.ncl.ac.uk/mep/2018/04/08/reproducible-publication-quality-multivariate-plots-in-r/.
And I obtained the codes to color up the geom_text from https://ggplot2.tidyverse.org/reference/geom_point.html, by doing so the factor(cyl) wasn't work out for me.
Have an assignment where we need to provide one-dimensional graphs for EDA but the sample code given answers most of the requirements already (simple scatter and box plots and a histogram) so I am trying to "spice it up" a little by creating some more interesting graphs. Only need a couple.
The data set is the twin IQ data across several studies/authors and I was wanting to do a back-to-back histogram of the twins separated by author. So far I can do an overlay of authors or the back to back of the twins using ggplot but I am then stuck when trying to separate in to either 4 graphs or overlaid back-to-backs.
The code I was using for the overlay was ggplot with either geom_density or geom_histogram and the code for the back-to-back came from R-Bloggers and I used the first snippet:
ggplot(df, aes(IQ)) + geom_histogram(aes(x = x1, y = ..density..), fill = "blue") + geom_histogram( aes(x = x2, y = -..density..), fill = "green")
What I am looking for is a way to combine these two techniques or how to get ggplot to split the graphs up by factor in much the same was as plot/lattice does when you do, for example:
bwplot(y~x1.x2|Author, data=df)
The snippet that I am using to achieve separate plots includes facet_grid() such that the final code is:
ggplot(df, aes(y)) + facet_grid(~Author) + geom_histogram(aes(x = x1, y = ..density..), fill = "green") + geom_histogram(aes(x = x2, y = -..density..), fill = "blue")
I wasn't previously aware of the facet_grid() function of ggplot so thank you very much to MLavoie and Brandon Bertelsen.
I built a pie chart using ggplot2 package but because some of the slices are very small the group labels overlap one another, and the value labels as well. Im looking for a way to get the labels furthere away from the slices and linking the slice and the label with a line.
Im using this data:
a<-c(0.5,0.01,2,50,40,7)
data<-data.frame(a)
data$b<-c("A","B","C","D","E","F")
and I used the following code:
p<- ggplot(data,aes(x=1,y=a,fill=b))
p<- p + geom_bar(stat = "identity",color="black")
p<- p+coord_polar("y")
br<-cumsum(data$a) - data$a/2
p<-p+theme(legend.position = "none",axis.text.x=element_text(color='black',size = 15))+
scale_y_continuous(breaks=br,labels=data$b)+
geom_text(aes(y = a/3 + c(0, cumsum(a)[-length(a)]),
label=a),size=6)
and the resaulted plot is:
and im looking for somthing similar to that one (that I found online):
This is very similar to this question (link), but I'm not quite sure how to manipulate it for my needs.
I have a faceted plot with two panels, and I would like to label three quadrants in the first panel and only the first panel.
Here is a mock data set:
dfr=data.frame(
variable=rep(c("A","B"),each=2),
x=c(2,-3,4,-5),
y=c(-2,4,-2,6))
And here is the plot:
p=ggplot(dfr,aes(x,y))+
geom_point()+
facet_grid(variable~.)+
scale_x_continuous(limits=c(-6,6))+
scale_y_continuous(limits=c(-6,6))+
geom_hline(yintercept=0)+
geom_vline(xintercept=0)
This is what I would like to accomplish:
You can always create a separate data frame with the desired labels and plot them using geom_text:
dfLab <- data.frame(variable = rep("A",3),
x = c(3,3,-3),
y = c(3,-3,-3),
lab = c('I','IV','III'))
ggplot(dfr,aes(x,y))+
geom_point()+
facet_grid(variable~.)+
scale_x_continuous(limits=c(-6,6))+
scale_y_continuous(limits=c(-6,6))+
geom_hline(yintercept=0)+
geom_vline(xintercept=0) +
geom_text(data = dfLab,aes(x=x,y=y,label=lab))