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):
Related
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)
Currently i have plotted a stacked bargraph to display the amount of people having delays in each month with the following code
q1%>%
mutate(DepDelay1 =factor(x=DepDelay1, levels=c(0,1), labels=c("No","Yes"))) %>%
filter(Year==2005)%>%
ggplot(aes(x=factor(Month), fill=DepDelay1)) +
geom_bar(position="stack") +
ggtitle("DepDelay of Flight") +
guides(fill=guide_legend("Delay")) +
xlab ("Month")+
ylab ("Flight Count")+
geom_text(aes(label=..count..),
stat='count',
colour = "white",
size = 2.5,
position = position_stack(vjust = 0.5))
Graph Example for position="stack"
However i cannot seem to produce a 100% stacked bargraph after changing from
geom_bar(position="stack")
to
geom_bar(position="fill")
Graph Example for position="fill"
This method works before i adjust the geom_text and adding the filter to my current code.
Anyone know what's the issue ? From what i understand, a Y-variable is needed however i don't have a suitable value to input it from the dataset and i'm trying to plot the count per month.
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 made a stacked barplot in ggplot2 in R:
ggplot(Count_dataframe_melt, aes(x = as.factor(variable), y = value, fill = fill)) +
geom_bar(stat = "identity",position="fill")+ scale_y_continuous(name = "Y-axis",labels = scales::percent)
I want to just visualize the top portion of the stacked barplot like so:
I've looked everywhere and can't figure out how to do this. Does anyone know how?
You can use coord_cartesian to "zoom in" on the area you want.
# your plot code...
ggplot(Count_dataframe_melt, aes(x = as.factor(variable), y = value, fill = fill)) +
geom_bar(stat = "identity",position="fill") +
scale_y_continuous(name = "Y-axis",labels = scales::percent) +
# set axis limits in coord_cartesian
coord_cartesian(ylim = c(0.75, 1))
Note that many people consider bar plots that don't start at 0 misleading. A line plot may be a better way to visualize this data.
Since the areas you want to show are less than 20% of the total area, you could flip the bar charts so that you only show the colors areas. Then the y-axis goes from 0-25% and you can use the figure caption to describe that the remaining data is in the gray category.
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.