Posts

Showing posts with the label ggplot2

Adding text annotation to a clustering scatter plot (tSNE)

Image
Adding text annotation to a clustering scatter plot (tSNE) I have XY data (a 2D tSNE embedding of high dimensional data) which I'd like to scatter plot . The data are assigned to several cluster s, so I'd like to color code the points by cluster and then add a single label for each cluster , that has the same color coding as the cluster s, and is located outside (as much as possible) from the cluster 's points. XY tSNE scatter plot cluster cluster cluster cluster cluster Any idea how to do this using R in either ggplot2 and ggrepel or plotly ? R ggplot2 ggrepel plotly Here's the example data (the XY coordinates and cluster assignments are in df and the labels in label.df ) and the ggplot2 part of it: XY cluster df label.df ggplot2 library(dplyr) library(ggplot2) set.seed(1) df <- do.call(rbind,lapply(seq(1,20,4),function(i) data.frame(x=rnorm(50,mean=i,sd=1),y=rnorm(50,mean=i,sd=1),cluster=i))) df$cluster <- factor(df$cluster) label.df <- data.frame(c...

How to zoom in on a specific range of values for a categorical variable in ggplot2?

Image
How to zoom in on a specific range of values for a categorical variable in ggplot2? I just want to zoom-in on the x axis between the values ford and nissan in the mpg dataframe. packageused: tidyverse But I am getting the following error when using coord_cartesian() function p<-ggplot(mpg,aes(x=manufacturer,y=class)) p+geom_point()+ + coord_cartesian(xlim = c('ford','nissan')) Error in +coord_cartesian(xlim = c("ford", "nissan")) : invalid argument to unary operator 1 Answer 1 You can use a function for contextual zoom from ggforce package ( facet_zoom ) to achieve this: ggforce facet_zoom # loading needed libraries library(ggplot2) library(ggforce) # selecting variables to display names <- as.vector(unique(mpg$manufacturer)) selected.names <- names[4:11] # zooming in on the axes ggplot(mpg, aes(x = manufacturer, y = class)) + geom_...

How can I change the thickness of a specific line and/or add shape in a multi-line plot?

Image
How can I change the thickness of a specific line and/or add shape in a multi-line plot? I have a data called molten.data below. I have this code which plots the lines I want, but I need to change the thickness of one specific line ( G11F:G11M ) to make it look thicker compared to other lines or preferably add shape to the data point in that line. How can we do it? molten.data G11F:G11M code I have: ggplot(molten.data, aes(variable, value,group= key.related,colour=key.related)) + geom_line() + geom_point() data: molten.data<- structure(list(key.related = c("G11F:G11F", "G11F:G11F", "G11F:G11F", "G11F:G11M", "G11F:G11F", "G11F:G11M", "G11F:AOGC-02-0079", "G11F:G11F", "G11F:G11M"), variable = structure(c(1L, 2L, 3L, 3L, 4L, 4L, 4L, 5L, 5L), .Label = c("IBS_2_samples", "IBS_4_samples", "IBS_8_samples", "IBS_16_samples", "IBS_32_samples"), ...