Soni Gupta
Soni Gupta

Reputation: 1

Label specific points in ggplot

I would like certain points I have created through ggplot to take labels at the side of the graph but I am not able to do that through my current code.

Ceplane1 is a matrix with two columns and 100 rows ( can take any random numbers). I want to plot column 2 on the x-axis and column 1 on the y-axis with. I have done this part using the below code. Now I want to make changes in the code so that I can put the label at the side of the graph and not on the graph area itself. Additionally, I want to represent the axis in a comma format. you can take result.table[1,1] and result.table[1,3] to be some number and suggest the solution.

ggplot(Ceplane1, aes(x = Ceplane1[,2], y = Ceplane1[,1])) +
  geom_point(colour="blue")+geom_abline(slope = -results.table[5,1],intercept = 0,colour="darkred",size=1.25)+
  geom_point(aes(mean(Ceplane1[,2]),mean(Ceplane1[,1])),colour="red")+
  geom_point(aes(results.table[1,1],results.table[3,1],colour="darkred"))+ggtitle("CE-Plane: Drug A vs Drug P")+
  xlab("QALY Difference")+ylab("Cost Difference")+xlim(-0.05,0.05)+ylim(-6000,6000)+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),plot.background = element_rect(fill = "white", colour = "black", size = 0.5))+
  geom_vline(xintercept = 0,colour="black")+geom_hline(yintercept = 0,colour="black")+
  geom_label(aes(mean(Ceplane1[,2]),mean(Ceplane1[,1])),label="mean")+
  geom_label(aes(results.table[1,1],results.table[3,1]),label="Base ICER")

I want to put the label at the side of the graph and not on the points of the graph itself. Please suggest me a way to do that.

Upvotes: 0

Views: 305

Answers (1)

Sven
Sven

Reputation: 1253

I think the best way is to add the mean and Base ICER points to your dataset. Then add a column for the legend and you will see them show up as matching in the chart and the legend:

library(ggplot2)

set.seed(1)
Ceplane1 <- data.frame(y = rnorm(100), 
                  x = rnorm(100))
results.table <- data.frame(z = rnorm(100))
Ceplane1$Legend <- "Data"

meanPoint <- data.frame(y = mean(Ceplane1[,1]), x = mean(Ceplane1[,2]), Legend = "Mean")
basePoint <- data.frame(y = results.table[3,1], x = results.table[1,1], Legend = "Base ICER")

Ceplane1 <- rbind(Ceplane1, meanPoint)
Ceplane1 <- rbind(Ceplane1, basePoint)

ggplot(Ceplane1, aes(x = x, y = y, color = Legend)) +
  geom_point() +
  geom_abline(slope = -results.table[5,1],intercept = 0,colour="darkred",size=1.25) +
  ggtitle("CE-Plane: Drug A vs Drug P")+ xlab("QALY Difference")+ylab("Cost Difference") +
  xlim(-3,3) + ylim(-3,3) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(),plot.background = element_rect(fill = "white", colour = "black", size = 0.5)) +
  geom_vline(xintercept = 0,colour="black") +
  geom_hline(yintercept = 0,colour="black")

This gives me the following:

enter image description here

Note that I changed the xlim and ylim to match the random data I created.

Upvotes: 1

Related Questions