1

I need to show only the elements of the y axis and nothing else.

name <- c("A", "B", "C", "D", "E")
values <- c(1, 2, 3, 4, 5)
data<-data.frame(name, values)

ggplot()
geom_blank(data=data, aes(y=reorder(name,desc(name))))+
theme(axis.text.x=element_blank(),
axis.title = element_blank(), 
axis.text.y=element_text(colour="black"))

This still shows the plot. I want to show only the y axis elements.

3 Answers3

1

If your aim is just to draw five letters without anything else, no need to bother with axes, but draw the labels directly.

ggplot() + 
  annotate(geom = "text", x = 1, y = 1:5, label = rev(LETTERS[1:5])) +
  theme_void()
tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Thanks! The current data is just an example. The data I'm working with has survey text. – Apoorva Hungund Feb 09 '23 at 10:24
  • @ApoorvaHungund the same principle would apply of course - if you just want to plot this text. Just change the vector argument for "label" – tjebo Feb 09 '23 at 10:26
  • Thanks! This helped tons. But annotate() seems to be switching the order of my labels.. reorder() isn't helping here, neither is factor(). Any tips on how to maintain a specific order? @tjebo – Apoorva Hungund Feb 09 '23 at 11:48
  • @ApoorvaHungund this depends on the order of your label vector - you see, I am using rev() in order to turn around the order of the letters. but this only works in this example. ggplot will plot from bottom to top - thus what comes first in your vector will be plotted at the bottom. – tjebo Feb 09 '23 at 12:16
  • 1
    Yes, that worked! Just made a "MyLabel" with the labels in reverse order. Thank you!! – Apoorva Hungund Feb 09 '23 at 12:38
0

Is this what you are looking for?

name <- c("A", "B", "C", "D", "E")
values <- c(1, 2, 3, 4, 5)
df1<-data.frame(name, values)

library(ggplot2)
library(dplyr)

ggplot(df1, aes(values, y=reorder(name,desc(name)))) +
  theme_void()+
  theme(axis.text.y=element_text(colour="black"))

Created on 2023-02-09 with reprex v2.0.2

Peter
  • 11,500
  • 5
  • 21
  • 31
  • not sure why you would need the element_blank calls at all, after using theme_void. – tjebo Feb 09 '23 at 10:02
  • Thanks! But I want only the y-axis elements, not even the plot. – Apoorva Hungund Feb 09 '23 at 10:23
  • @tjebo, I didn't delete the OPs code, removed superfluous code. – Peter Feb 09 '23 at 11:01
  • did you mean to say that you removed code from your answer? I still see those mentioned unnecessary lines. I think your solution is generally nice, but because of those element_blank lines it's losing it's elegance. And a few words of explaining would also be nice. – tjebo Feb 09 '23 at 13:55
0

This will get you most of the way there:

ggplot()+
geom_blank(data=data, aes(y=reorder(name,desc(name))))+
  theme_minimal()

enter image description here

Then you just need to drop the gridlines and label:

ggplot()+
geom_blank(data=data, aes(y=reorder(name,desc(name))))+
  theme_minimal()+
  theme(panel.grid = element_blank(),
        axis.title = element_blank())

enter image description here