Search code examples
rplotstatisticspolygon

How to delete values in Axis x of plots using R


I have a simple dataframe like this

data_umur = data.frame(
kategori_umur = c("18-23", "23-28", "28-33", "33-38","38-43", "43-48", "48-53"),
frekuensi = c(257,73,103,15,36,14,2),
mid_kelompok_umur = c(20.5, 25.5, 30.5, 35.5, 40.5, 45.5, 50.5),
c_i = c(0, 1,2,3,4,5,6)
)

I want to make a polygon frequency, and here are the codes

plot(x = 1:length(data_umur$kategori_umur), y = data_umur$frekuensi, ylab = "Frekuensi", xlab = "Kategori Umur", main = "Poligon Frekuensi untuk Distribusi Umur Mahasiswa Prodi SI")
axis(1, at = 1:length(data_umur$kategori_umur), labels = data_umur$kategori_umur)
points(x = 1:length(data_umur$kategori_umur), y = data_umur$frekuensi, pch = 16, col = "blue") 
lines(x = 1:length(data_umur$kategori_umur), y = data_umur$frekuensi)

This is the plot I get polygon

I have tried to get rid the axis value 1:7 and replace them by "Kategori Umur", but they are still overlapping. Hot to delete the axis values (labels 1, 2, 3, 4, 5, 6, 7)?


Solution

  • Try omitting the x-axis in the first plot() using xaxt = "n":

    plot(x = 1:length(data_umur$kategori_umur), 
         y = data_umur$frekuensi, 
         ylab = "Frekuensi", 
         xlab = "Kategori Umur", 
         main = "Poligon Frekuensi untuk Distribusi Umur Mahasiswa Prodi SI", 
         xaxt = "n")
    

    Then use axis(), points() and lines() as before.

    Result:

    enter image description here