我的数据存储在csv格式中。我想根据活动绘制这些数据,这意味着4种不同的活动应该是4种不同的颜色。
ACTIVITY LAT LONG
Resting 21.14169444 70.79052778
Feeding 21.14158333 70.79313889
Resting 21.14158333 70.79313889
Walking 21.14163889 70.79266667
Walking 21.14180556 70.79222222
Sleeping 21.14180556 70.79222222
我尝试过以下代码,但不起作用:
ACTIVITY.cols <- cut(ACTIVITY, 5, labels = c("pink", "green", "yellow","red","blue"))
plot(Data$Latitude,Data$Longitude, col = as.character(ACTIVITY.cols)
和
plot(Data$Latitude,Data$Longitude, col=c("red","blue","green","yellow")[Data$ACTIVITY]
答案 0 :(得分:2)
使用
txt <- "ACTIVITY LAT LONG
Resting 21.14169444 70.79052778
Feeding 21.14158333 70.79313889
Resting 21.14158333 70.79313889
Walking 21.14163889 70.79266667
Walking 21.14180556 70.79222222
Sleeping 21.14180556 70.79222222"
dat <- read.table(text = txt, header = TRUE)
一种选择是使用nlevels(ACTIVITY)
变量作为索引,索引长度为ACTIVITY
的颜色矢量。
cols <- c("red","green","blue","orange")
plot(LAT ~ LONG, data = dat, col = cols[dat$ACTIVITY], pch = 19)
legend("topleft", legend = levels(dat$ACTIVITY), col = cols, pch = 19, bty = "n")
这会产生
要了解其工作原理,cols
已扩展为
> cols[dat$ACTIVITY]
[2] "green" "red" "green" "orange" "orange" "blue"
因为ACTIVITY
是一个因素,但在数字上存储为1,2,...,n。
其他更高级别的解决方案可用,因此请考虑使用 ggplot2 包来简单创建相同的图表。
library("ggplot2")
plt <- ggplot(dat, aes(x = LONG, y = LAT, colour = ACTIVITY)) +
geom_point()
plt
产生
答案 1 :(得分:1)
以下是我如何使用命名向量来定义颜色:
$cartitems = count ($_SESSION['cart']['products'])
+ count ($_SESSION['cart']['addons'])
+ count ($_SESSION['cart']['domains']);
答案 2 :(得分:0)
使用ggplot2包它更快更美观。
function openPicker(inputDateElem) {
var ev = document.createEvent('KeyboardEvent');
ev.initKeyboardEvent('keydown', true, true, document.defaultView, 'F4', 0);
inputDateElem.dispatchEvent(ev);
}
var cal = document.querySelector('#cal');
cal.focus();
openPicker(cal);