简体   繁体   English

更改R图中的颜色

[英]Change colors in r plot

I am currently trying to plot some data and don't manage to obtain a nice result. 我目前正在尝试绘制一些数据,但无法获得良好的结果。 I have a set of 51 individuals with each a specific value (Pn) and split within 14 groups. 我有一组51个人,每个人都有特定的值(Pn),分为14组。 The closest thing I end up with is this kind of plot . 我最后得到的最接近的东西就是这种情节 I obtain it thanks to the simple code bellow, starting by ordering my values for the Individuals : 我可以通过以下简单的代码获得它,首先是为个人设置值:

Individuals <- factor(Individuals,levels=Individuals[order(Pn)])
dotchart(Pn,label=Individuals,color=Groups)

The issue is that I only have 9 colors on this plot (so I lost information somehow) and I can't manage to find a way to apply manually one color per group. 问题是我在该图上只有9种颜色(因此我以某种方式丢失了信息),而且我无法设法找到一种手动为每个组应用一种颜色的方法。

I've also try to use the ggplot2 package by reading it could give nice looking things. 我还尝试通过阅读ggplot2包来提供美观的东西。 In that case I can't manage to order properly the Individuals (the previous sorting doesn't seem to have any effect here), plus I end up with only different type of blue for the group representation which is not an efficient way to represent the information given by my data set. 在那种情况下,我无法正确地对个人进行排序(以前的排序似乎在这里没有任何作用),而且我最终只能为组表示提供不同类型的蓝色,这不是一种有效的表示方法我的数据集提供的信息。 The plot I get is accessible here and I used the following code: 我得到的图可以在这里访问,我使用了以下代码:

ggplot(data=gps)+geom_point(mapping=aes(x=Individuals, y=Pn, color=Groups))

I apologize if this question seems redundant but I couldn't figure a solution on my own, even following some answer given to others... 如果这个问题看起来很多余,我深表歉意,但是即使有人给出了一些答案,我也无法自行解决。

Thank you in advance! 先感谢您!

EDIT: Using the RColorBrewer as suggested bellow sorted out the issue with the colors when I use the ggplot2 package. 编辑:当我使用ggplot2包时, 按照下面的建议使用RColorBrewer 来解决颜色问题

I can't be sure without looking at your data, but it looks like Groups may be a numeric value. 我无法确定是否不查看您的数据,但看起来Groups可能是数字值。 Try this: 尝试这个:

gps$Groups <- as.factor(gps$Groups)

library(RColorBrewer)
ggplot(data=gps)+
geom_point(mapping=aes(x=Individuals, y=Pn, color=Groups))+ 
scale_colour_brewer(palette = "Set1")

I believe you are looking for the scale_color_manual() function within ggplot2 . 我相信你正在寻找scale_color_manual()内的功能ggplot2 You didn't provide a reproducible example, but try something along the lines of this: 您没有提供可复制的示例,但可以尝试以下方法:

ggplot(data=gps, mapping=aes(x=Individuals, y=Pn, color=Groups))+
    geom_point() +
    scale_color_manual(values = c('GROUP1' = 'color_value_1',
                                  'GROUP2' = 'color_value_2',
                                  'GROUP3' = 'color_value_3'))

Replace GROUPX with the values inside your Group column, and replace color_value_x with whatever colors you want to use. GROUPX替换为“组”列中的值,然后将color_value_x替换为您要使用的任何颜色。

A good resource for further learning about ggplot2 is chapter 3 of R For Data Science , which you can read here: http://r4ds.had.co.nz/data-visualisation.html R For Data Science的第3章是进一步了解ggplot2资源,您可以在此处阅读: http : ggplot2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM