Reputation: 69
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!
Upvotes: 3
Views: 988
Reputation: 39595
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:
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
))
Upvotes: 2