3

The sample of my dataset is this: Data

I am quite amateur at this and have written the following code to print the results out with ggplot:

mach <- structure(list(n = 1:9, Actual = c(330, 525, 588, 590, 425, 365, 
880, 512, 275.7), ANFIS = c(350L, 500L, 600L, 623L, 423L, 369L, 
876L, 517L, 232L), Fuzzy = c(400L, 575L, 555L, 600L, 400L, 352L, 
856L, 784L, 276L), MLR = c(325L, 600L, 499L, 620L, 389L, 358L, 
877L, 321L, 208L), ANN = c(369L, 360L, 620L, 675L, 477L, 366L, 
652L, 513L, 269L)), class = "data.frame", row.names = c(NA, -9L
))


l= c(1,9)
MRR= expression(bold("MRR (mm"^3*"min"^-1*")"))

ggplot() +
  geom_line(data=mach, aes(x=n, y=Actual, color='Actual')) +
  geom_line(data=mach, aes(x=n, y=ANFIS, color='ANFIS')) +
  geom_line(data=mach, aes(x=n, y=Fuzzy, color='Fuzzy')) +
  geom_line(data=mach, aes(x=n, y=MLR, color='MLR')) +
  geom_line(data=mach, aes(x=n, y=ANN, color='ANN')) +
  geom_point(data = mach, shape=1, aes(x=n, y=Actual)) +
  geom_point(data = mach, shape=2, aes(x=n, y=ANFIS)) +
  geom_point(data = mach, shape=11, aes(x=n, y=Fuzzy)) +
  geom_point(data=mach, shape=4, aes(x=n,y=MLR))+
  geom_point(data = mach, shape=5, aes(x=n, y=ANN))+
  theme(legend.title = element_blank(),
        legend.position = c(.94, .86),
        axis.title.x = element_text(size = 11, face = 'bold'),
        axis.title.y = element_text(size = 11, face = 'bold'),
        legend.box.background = element_rect(colour = "black"),
        legend.background = element_blank(),
        panel.background = element_rect(colour = "black",
                                        size = 2, linetype = "solid")) +
  scale_x_continuous(breaks = round(seq(min(mach$n), max(mach$n), by = 1),1)) +
  labs(y=MRR, x='Number of testing points') 

The output is this:Output

I don't have much problem because I only have to present the graph here. But is there any way to shorten my code? It feels quite redundant with many 'geom_line' and 'geom_point'.

Any help would mean a lot!

3
  • 1
    Images are not a good way for posting data (or code). Relevant xkcd. Can you post sample data in dput format? Please edit the question with the output of dput(mach). Or, if it is too big with the output of dput(head(mach, 20)). Commented Jan 17, 2021 at 20:17
  • In addition to @RuiBarradas, also the following reasons. Commented Jan 17, 2021 at 20:18
  • Hey! @RuiBarradas pardon me for the inconvenience. I have added the data in dput format now. Commented Jan 17, 2021 at 20:33

1 Answer 1

2

Try this. You can reshape your data in order to avoid adding layer by layer. Also next time use dput() an do not use screenshots to share data as sometimes it can be complex to copy by hand your data. Here the code:

library(dplyr)
library(tidyr)
library(ggplot2)
#Inputs
l= c(1,9)
MRR= expression(bold("MRR (mm"^3*"min"^-1*")"))
#Code
mach %>% pivot_longer(-1) %>%
  ggplot(aes(x=n,y=value,color=name,shape=name))+
  geom_line()+
  geom_point()+
  scale_shape_manual(values=c(1,2,5,11,4))+
  theme(legend.title = element_blank(),
        legend.position = c(.94, .86),
        axis.title.x = element_text(size = 11, face = 'bold'),
        axis.title.y = element_text(size = 11, face = 'bold'),
        legend.box.background = element_rect(colour = "black"),
        legend.background = element_blank(),
        panel.background = element_rect(colour = "black",
                                        size = 2, linetype = "solid")) +
  scale_x_continuous(breaks = round(seq(min(mach$n), max(mach$n), by = 1),1)) +
  labs(y=MRR, x='Number of testing points')

Output:

enter image description here

Some data used:

#Data
mach <- structure(list(n = 1:9, Actual = c(330, 525, 588, 590, 425, 365, 
880, 512, 275.7), ANFIS = c(350L, 500L, 600L, 623L, 423L, 369L, 
876L, 517L, 232L), Fuzzy = c(400L, 575L, 555L, 600L, 400L, 352L, 
856L, 784L, 276L), MLR = c(325L, 600L, 499L, 620L, 389L, 358L, 
877L, 321L, 208L), ANN = c(369L, 360L, 620L, 675L, 477L, 366L, 
652L, 513L, 269L)), class = "data.frame", row.names = c(NA, -9L
))
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Duck ! It even added the points in the legend which I was looking to do to. One query, how does pivot_longer(-1) helped here? I am in a bit of oblivion here
@ShibaprasadBhattacharya Hi dear. The pivot is keeping first column and moving the other columns ar rows so that the plot can be sketched in an easy way!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.