替换图例并在散点图上创建不同的颜色

时间:2015-08-04 13:16:22

标签: r plot

我正在研究全国范围内的数据,并试图研究疾病数量和鸡群大小之间的关系。我想更改散点图的图例,即为区域命名而不是在此处发布的图上显示的代码。我还想对代表8个区域的颜色进行一些改进,以便有一些明显的差异因为有点难以区分当前的颜色。关于在情节上进行改进的任何建议?

library(lattice)
xyplot(log(Cases2012+1)~ Flock2012, data=orf, groups = Region.Coding, 
      auto.key =
         list(space = "right", points = TRUE))

部分数据:

Region Flock2012
    1   190
    2   343
    1   810
    3   1450
    1   1125
    3   1305
    1   750
    1   227
    3   1800
    1   1100
    2   1250
    1   362
    6   800
    2   559
    4   770
    1   900
    2   600
    1   860
    2   1450
    6   1014
    1   1870
    4   950
    1   1730
    5   353
    1   6000
    5   1150
    1   3100
    1   2400
    5   278
    2   444
    2   546
    7   775
    2   870
    5   690
    8   1032
    2   2351
    7   680
    3   430
    2   931
    8   1590
    2   70
    5   780
    2   1366
    2   1900
    4   730
    2   1860
    2   1032
    7   1700
    2   230
    2   301
    5   565

Scatter plot for cases and flock 2012

试过这个,但情节没有出现

mycols <- c("red", "blue", "forestgreen", "gold", "black", "cyan", "darkorange", "darkred")
myregions <- c("East", "Midlands", "Wmidlands","NWest","NEast","Yorkshire","SEast","SWest")
xyplot(log(Flock2012+1)~ Flock2012, data=stack, groups = Regions, 
       col=mycols, pch=1,
       key=list(space="right",
                text=list(myregions),
                points=list(col=mycols, cex=1.5, pch=1)

1 个答案:

答案 0 :(得分:1)

我认为这应该有效。我会创建一个你想要的颜色列表和一个区域名称列表。

mycols <- c("red", "blue", "forestgreen", "gold", "black", "cyan", "darkorange", "darkred")
myregions <- c("East", "Midlands", "Wmidlands","NWest","NEast","Yorkshire","SEast","SWest")

然后使用密钥选项而不是使用auto.key选项,以获得更大的灵活性。

xyplot(log(Cases2012+1)~ Flock2012, data=orf, groups = Region.Coding, 
   col=mycols, pch=1,
key=list(space="right",
     text=list(myregions),
     points=list(col=mycols, cex=1.5, pch=1)))

enter image description here

希望这有帮助。