Create a line graph showing a 5-day temperature trend using the vector temps <- c(22,24,23,26,25)
Requirements:-
Set the line type to solid with points at each data value
Color the line red and make it twice the default thickness
Add a title "Weekly Temperature" and label the axes "Day" and "Temp (C)
Code:-
temps <- c(22, 24, 23, 26, 25)
plot(temps, type = "o",
col = "red",
lwd = 2,
main = "Weekly Temperature",
xlab = "Day",
ylab = "Temp (C)",
xlim=c(1,8),
ylim=c(10,30),
lty="dotted",
cex=0.6, #size of dot
pch=5) #different symbol in range 1-25
Solution:-
Explanation:-
All parameter of plot():-
Example-2:- If we have multiple line in question then-- y1 <- c(2, 4, 6, 8, 10),
y2 <- c(1, 3, 5, 7, 9), y3 <- c(5, 4, 3, 2, 1)
Code:-
# Define your data
y1 <- c(2, 4, 6, 8, 10)
y2 <- c(1, 3, 5, 7, 9)
y3 <- c(5, 4, 3, 2, 1)
# Create the main plot
plot(y1, type="b", col="blue", pch=16, ylim=c(0, 10), ylab="Value", main="Base R Plot")
#If we have to add more line then we use only line command instead of plot
lines (y2, type="b", col="red", pch=17)
lines (y3, type="b", col="green", pch=18)
# Add a legend
legend=c("y1", "y2", "y3"), col=c("blue", "red", "green"), pch=16:18, lty=1,
inset=c(-0.35, 0), cex=0.8)
Solution:-
Explanation:-
- In 2nd, 3rd, 4th line we just put the data which given in question
- To add the line first we use plot command, as you can see above I explain all the parameter in a table , pch= mainly for different symbol (Put value in between 1-25)
- To add more line then we use only "lines" command instead of plot here
- In last command we use legend to create a seperate box to show the data, but when you try to put the legend box out of the main box it not appear (see next example)
Example-3:- Problem in put the legend box out of the main box
Code:-
# Define your data
y1 <- c(2, 4, 6, 8, 10)
y2 <- c(1, 3, 5, 7, 9)
y3 <- c(5, 4, 3, 2, 1)
# Create the main plot
plot(y1, type="b", col="blue", pch=16, ylim=c(0, 10), ylab="Value", main="Base R Plot")
#If we have to add more line then we use only line command instead of plot
lines (y2, type="b", col="red", pch=17)
lines (y3, type="b", col="green", pch=18)
# Add a legend
legend=c("y1", "y2", "y3"), col=c("blue", "red", "green"), pch=16:18, lty=1,
inset=c(-0.02, 0.4), cex=0.5)
Result:-
Explanation:- As you can see here when have change the inset value, legend box not seen completely. To fix this problem we use par(xpd=TRUE) , check in next example
Example-4:- Legend box out of the main box
Code:-
# Define your data
y1 <- c(2, 4, 6, 8, 10)
y2 <- c(1, 3, 5, 7, 9)
y3 <- c(5, 4, 3, 2, 1)
# Create the main plot
plot(y1, type="b", col="blue", pch=16, ylim=c(0, 10), ylab="Value", main="Base R Plot")
# If want to put legend box out of the main box
par(xpd=TRUE)
#If we have to add more line then we use only line command instead of plot
lines (y2, type="b", col="red", pch=17)
lines (y3, type="b", col="green", pch=18)
# Add a legend
legend=c("y1", "y2", "y3"), col=c("blue", "red", "green"), pch=16:18, lty=1,
inset=c(-0.02, 0.4), cex=0.5)
Result:-
Explanation:- In this we use par(xpd=TRUE) to visualize the legend box out of the main box, but you see one problem here that space on the right side is not so much that I put legend box. So, either I have to decrease the font size of legend box by cex command (but that cause font size small and difficult to visualize) or we use par (mar=c ()) command see in next example
Example-5:- To create more space or more data
Code:-
y1 <- c(2, 4, 6, 8, 10)
y2 <- c(1, 3, 5, 7, 9)
y3 <- c(5, 4, 3, 2, 1)
# Create the main plot
plot(y1, type="b", col="blue", pch=16, ylim=c(0, 10), ylab="Value", main="Base R Plot")
# If want to put legend box out of the main box
par(xpd=TRUE)
# If you want space on right side as you can see in the image there is no space to put legend box out of the main box
par(mar=c(2, 2, 2, 4) +0.1)
#If we have to add more line then we use only line command instead of plot
lines (y2, type="b", col="red", pch=17)
lines (y3, type="b", col="green", pch=18)
# Add a legend
legend=c("y1", "y2", "y3"), col=c("blue", "red", "green"), pch=16:18, lty=1,
inset=c(-0.02, 0.4), cex=0.5)
Result:-
The command par(mar=c(2, 2, 2, 4) +0.1)
- 0.1 in last is optional you can avoid this
- As you can see "4" in par(mar=c(2,2,2,4) denote the right side , which create space in the right side of image so you put legend box in that space





No comments:
Post a Comment