What would you like to communicate with the plot?
For a summary of the dataset, you can think about a scatter plot with x = unique no patients, y = no of units prescribed and put a label close to each point representing the drug id.
This is an example with the dataset mtcars
library("ggplot2")
data("mtcars")
mtcars$mdl <- rownames(mtcars)
ggplot(mtcars, aes(x=mpg, y=wt, label=mdl)) +
geom_point() +
geom_text(check_overlap=TRUE, vjust="inward", hjust="inward")
Set check_overlap=FALSE
if you want to see all (overlapping) labels.
Since you have a large number of points, you may want to label just those that can be informative about how the data is distributed.
To do that, you can do a scatter plot
plot(x=mtcars$mpg, y=mtcars$wt)
then use identify
to click on the points you want to label
identify(x=mtcars$mpg, y=mtcars$wt, labels=mtcars$mdl, cex=0.8)
You have to click on the points that you want to label, then press ESC
.
If you want to do it programmatically, you can set all labels to NA
except those that you want to show:
mtcars$mdl_1 <- mtcars$mdl
mtcars$mdl_1[-sample(nrow(mtcars), 5)] <- NA # Select 5 random to keep
ggplot(mtcars, aes(x=mpg, y=wt, label=mdl_1)) +
geom_point() +
geom_text(check_overlap=FALSE,vjust="inward",hjust="inward")
More than 2 variables:
In this case, you can use pairs
or ggpairs
from GGally
package
library(GGally)
mtcars$mdl <- rownames(mtcars)
mtcars$mdl_1 <- mtcars$mdl
mtcars$mdl_1[-sample(nrow(mtcars), 5)] <- NA ## Showing 5 random labels
gg <- ggpairs(data=mtcars, columns=c(1:5), aes(label=mdl_1))
for (i in 1:25) { ## 5*5 plots
for (j in 1:25) {
if (j < i) { ## lower half
plt <- getPlot(gg,i,j) + geom_text(size=3) + geom_point(size=1)
gg <- putPlot(gg,plt,i,j)
}
}
}
gg
where I've customized the scatter plots using the method described here.