Hi there, I'm wondering why my code below makes the legend coloured, but the dots themselves are not:
# dataset <- data.frame(IDName, Value, Setpoints)
# dataset <- unique(dataset)
# Paste or type your script code here:
dat <- aggregate(Value ~ Setpoints + IDName, dataset, mean)
x <- dat$Value
y <- dat$Setpoints
z <- dataset$IDName
plot(x,y, main ="Turbidity Frequency Distribution",xlab="% Time < Turbidity level", ylab="Turbidity (NTU)")
lines(spline(x,y))
palette()
legend('topleft', legend = unique(z), col = 1:3, cex = 0.8, pch = 1)
#constant lines
abline(h=c(0.1,0.15,0.3), col=c("red","pink","purple"), lty=2, lwd=3)
Make sure that z
is a factor
. Then, use col = z
when you create the plot. You will get colored points.
In your legend
(the character values to appear in legend) to the levels
of your factor
z
. In addition, set the colors based on unique(z)
- they should match your points.
Here is the complete example. In the future, instead of putting data in a comment, please edit your question with the data. Also, you may want to consider ggplot2
for future plotting.
dat <- aggregate(Value ~ Setpoints + IDName, dataset, mean)
x <- dat$Value
y <- dat$Setpoints
z <- dataset$IDName
z <- factor(z)
plot(x, y,
main ="Turbidity Frequency Distribution",
xlab="% Time < Turbidity level",
ylab="Turbidity (NTU)",
col = z)
lines(spline(x,y))
palette()
legend('topleft',
legend = levels(z),
col = unique(z),
cex = 0.8,
pch = 1)
#constant lines
abline(h=c(0.1,0.15,0.3), col=c("red","pink","purple"), lty=2, lwd=3)
Plot
Data
dataset <- structure(list(IDName = c("Filter01", "Filter01", "Filter01",
"Filter01", "Filter01", "Filter02", "Filter02", "Filter02", "Filter02",
"Filter02"), Setpoints = c(0.16, 0.2, 0.3, 2, 2.2, 0.16, 0.2,
0.3, 2, 2.2), Value = c(96.1, 96.2, 96.428, 99.603, 99.6, 98.8,
98.9, 99.049, 99.194, 99.2)), class = "data.frame", row.names = c(NA,
-10L))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.