bic ton
bic ton

Reputation: 1408

plot names of columns in R?

This is a subset of my dataset:

structure(list(zone = c(3L, 4L, 2L), la3 = c(1, 
6, 3), la4 = c(3, -2, 5)), row.names = c("1", 
"2", "3"), class = "data.frame")

How I can plot (ggplot2) them as the names of columns (la3 and la4)on x axis and zone on y axis?

Upvotes: 0

Views: 48

Answers (1)

Jet
Jet

Reputation: 690

You need to transform your data to "long" format first if you want to plot la3 and la4 on the same plot. You can add labels using geom_text or geom_label, but I highly recommend using the package ggrepel to add labels to a plot, and the functions geom_text_repel or geom_label_repel.

library(tidyverse)
library(ggrepel)

ggplot(data %>% gather(key=la, ...=-zone)) + 
geom_point(aes(la, value, color=as.character(zone))) +
geom_text_repel(aes(la, value, label=zone))

enter image description here

Upvotes: 1

Related Questions