如何使用ggplot2 + directlabels为标签使用自定义名称 [英] How to use custom names for labels with ggplot2 + directlabels

查看:189
本文介绍了如何使用ggplot2 + directlabels为标签使用自定义名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 geom_line()图中使用ggplot2和directlabels包,我希望其中一个标签可以读取X-M。但是,在我的 data.frame()XM中,列名称被重命名为XM,并且我无法找到有关如何提供<$ c使用自定义标签名称的$ c> direct.label 函数,也不读取帮助的源代码。 (直接标签似乎不符合在ggplot规模中设置的标签名称,这是我尝试的第一件事。)



示例代码:

  library(scales)
library(reshape2)
library(ggplot2)
library (直接标签)

data = data.frame(
C = c(1.2,1.4,0.3,-2.0,0.5),
I = c(1.2,1.5 ,-1.3,-3.8,1.8),
G = c(0.2,0.3,0.3,0.2,0.2),
XM= c(2.9,-0.7,0.3,-2.8,1.5 )+
c(-2.7,0.2,0.4,3.6,-2.4),
year = c(2006,2007,2008,2009,2010))

p < - ggplot(data = melt(data),aes(year,value,color = variable))+
geom_line(aes(group = variable))+
scale_color_hue (break = c(C,I,G,XM),
labels = c(C,I,G,XM))#directlabels doesn 't
#使用这个

#比较:
p

#with:
direct.label(p,list(last.points ,hjust = - 0.25))

可以看到结果图这里。带有直接标签的人使用X.M而不是X-M。非常感谢!

解决方案

直接标签似乎获得标签来自数据中的列名称。



这意味着您必须确保标签在数据中的正确性。为此,在创建 data.frame 时必须设置 check.names = FALSE

  data = data.frame(
C = c(1.2,1.4,0.3,-2.0,0.5),$ b $ (1.2,1.5,-1.3,-3.8,1.8),
G = c(0.2,0.3,0.3,0.2,0.2),
XM= c(2.9, - 0.7,0.3,-2.8,1.5)+
c(-2.7,0.2,0.4,3.6,-2.4),
year = c(2006,2007,2008,2009 ,2010),
check.names = FALSE)

数据
CIG XM年
1 1.2 1.2 0.2 0.2 2006
2 1.4 1.5 0.3 -0.5 2007
3 0.3 -1.3 0.3 0.7 2008
4 -2.0 -3.8 0.2 0.8 2009
5 0.5 1.8 0.2 -0.9 2010

现在绘制:

  p < -  ggplot( data = fusion(data),aes(year,value,color = variable))+ 
geom_line(aes(group = variable))
direct.label(p,list(last.points,hjust = -0.25))


I'm using ggplot2 with the directlabels package in a geom_line() plot, and I would like one of the labels to read "X-M". However, in my data.frame() "X-M" as column name gets renamed to "X.M", and I couldn't find documentation on how to provide the direct.label function with custom labels names, nor reading the source helped. (directabels doesn't seem to honor the label names set in the ggplot scale, which is the first thing that I tried.)

Sample code:

library("scales")
library("reshape2")
library("ggplot2")
library("directlabels")

data = data.frame(
  C = c(1.2, 1.4, 0.3, -2.0, 0.5),
  I = c(1.2, 1.5, -1.3, -3.8, 1.8),
  G = c(0.2, 0.3, 0.3, 0.2, 0.2),
  "X-M" = c(2.9, -0.7, 0.3, -2.8, 1.5) +
          c(-2.7, 0.2, 0.4, 3.6, -2.4),
  year = c("2006", "2007", "2008", "2009", "2010"))

p <- ggplot(data = melt(data), aes(year, value, color = variable)) +
  geom_line(aes(group = variable)) +
  scale_color_hue(breaks = c("C", "I", "G", "X.M"),
                  labels = c("C", "I", "G", "X-M"))  # directlabels doesn't
                                                     # use this

# Compare:
p

# with:
direct.label(p, list(last.points, hjust = -0.25))

Resulting graphs can be seen here. The one with directlabels uses "X.M" instead of "X-M". Many thanks in advance!

解决方案

The package directlabels seems to get the labels from the column names in your data.

This means you have to make sure your labels are correct in the data to start with. To do this, you have to set check.names=FALSE when you create the data.frame:

data = data.frame(
  C = c(1.2, 1.4, 0.3, -2.0, 0.5),
  I = c(1.2, 1.5, -1.3, -3.8, 1.8),
  G = c(0.2, 0.3, 0.3, 0.2, 0.2),
  "X-M" = c(2.9, -0.7, 0.3, -2.8, 1.5) +
    c(-2.7, 0.2, 0.4, 3.6, -2.4),
  year = c("2006", "2007", "2008", "2009", "2010"),
  check.names=FALSE)

data
     C    I   G  X-M year
1  1.2  1.2 0.2  0.2 2006
2  1.4  1.5 0.3 -0.5 2007
3  0.3 -1.3 0.3  0.7 2008
4 -2.0 -3.8 0.2  0.8 2009
5  0.5  1.8 0.2 -0.9 2010

Now plot:

p <- ggplot(data = melt(data), aes(year, value, color = variable)) +
  geom_line(aes(group = variable)) 
direct.label(p, list(last.points, hjust = -0.25))

这篇关于如何使用ggplot2 + directlabels为标签使用自定义名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆