plot multiple column in a point and connect it with line

I have a set of data with multiple columns as this

CW BF TF CE
Cla 52095 12456 2265 0
Clb 28312 1132 0 0
Clc 46430 39640 0 3397
Cld 20385 5660 0 0

and want to plot as attached graph.Capture

For your future posts, please provide your dataset in a copy-paste friendly format. You can use dput or datapasta for that. It'll be also preferable if you provide a reproducible example, as it shows your efforts and problems that you faced, and hence it helps people to help you. Please go through FAQ: How to do a minimal reproducible example ( reprex ) for beginners.

dataset <- data.frame(CW = c(52095, 28312, 46430, 20385),
                      BF = c(12456, 1132, 39640, 5660),
                      TF = c(2265, 0, 0, 0),
                      CE = c(0, 0, 3397, 0),
                      row.names = c("Cla", "Clb", "Clc", "Cld"))

matplot(x = dataset,
        type = "o",
        lty = 1,
        lwd = 2,
        pch = 19,
        xlab = "Class",
        ylab = "Value")
legend(x = "top",
       legend = names(x = dataset),
       col = 1:4,
       lty = 1,
       lwd = 2,
       pch = 19,
       ncol = 4)

Created on 2019-06-18 by the reprex package (v0.3.0)

You can rotate the y-axis labels using las = 1 and discard the box containing the legend using bty = "n".

Hope this helps.

1 Like

Thanks Yarnabrina, it helps but how do i replace xlabs (1,2,..) to Cla and clb

You can do it will ggplot2 like this:

library(tidyverse)

dataset <- data.frame(CW = c(52095, 28312, 46430, 20385),
                      BF = c(12456, 1132, 39640, 5660),
                      TF = c(2265, 0, 0, 0),
                      CE = c(0, 0, 3397, 0),
                      row.names = c("Cla", "Clb", "Clc", "Cld"))

dataset %>% 
  rownames_to_column() %>% 
  gather(key = key, value = value, CW:CE) %>% 
  mutate(rowname = factor(rowname)) %>% 
  ggplot(aes(as.numeric(rowname), value, color = key)) + 
  geom_point() + 
  geom_line() +
  scale_x_continuous(name = "Class", labels = c("Cla", "Clb", "Clc", "Cld")) +
  theme_bw()

Created on 2019-06-18 by the reprex package (v0.2.0).

1 Like

Thanks tbradley, this is perfect.

You can use xaxt and axis in base plot system.

dataset <- data.frame(CW = c(52095, 28312, 46430, 20385),
                      BF = c(12456, 1132, 39640, 5660),
                      TF = c(2265, 0, 0, 0),
                      CE = c(0, 0, 3397, 0),
                      row.names = c("Cla", "Clb", "Clc", "Cld"))

matplot(x = dataset,
        type = "o",
        lty = 1,
        lwd = 2,
        pch = 19,
        xlab = "Class",
        ylab = "Value",
        xaxt = "n")
axis(side = 1,
     at = 1:4,
     labels = rownames(x = dataset))
legend(x = "top",
       legend = names(x = dataset),
       col = 1:4,
       lty = 1,
       lwd = 2,
       pch = 19,
       ncol = 4,
       bty = "n")

Created on 2019-06-18 by the reprex package (v0.3.0)

Now that your problem is solved, will you please consider marking this thread as solved?

If you don't know how to do it, please take a look at this thread:

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.