ggplot2:仅显示一组中的文本标签 [英] ggplot2: display text labels from one group only

查看:48
本文介绍了ggplot2:仅显示一组中的文本标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置:

我有一小撮篮球运动员及其统计资料.

I have a tibble of basketball players and their statistics.

library(tidyverse)

df <- tibble(
  season = c(2010, 2011, 2012, 2013, 2014,
             2010, 2011, 2012, 2013, 2014),
  player = c("player_a", "player_a", "player_a", "player_a", "player_a",
             "league_avg", "league_avg", "league_avg", "league_avg", "league_avg"),
  fg_perc = c(.4912, .6083, .3095, .5525, .4289,
              .4825, .4836, .4819, .4860, .4848),
  points_game = c(20, 18, 15, 19, 18,
                  12, 12, 13, 11, 12)
)

我已经为player_a和League_avg显示了特定的列(fg_perc)作为geom_line().我还将它包装在一个自定义函数中,因为我将对其他统计信息使用相同的方法.

I've displayed a certain column (fg_perc) as a geom_line() for both player_a and league_avg. I've also wrapped it in a custom function, as I'll be using the same method for other statistics.

make_chart <- function(target_column) {
  df %>% 
    ggplot(aes_string("season", target_column, label = target_column)) + 
    geom_line(aes(color = player), size = 1.33)
}

make_chart("fg_perc")

我的问题:

我只想将fg_perc值显示为player_a数据的百分比.我无法弄清楚如何消除不了图例(当我传播()League_avg时).我正在寻找一种解决方案,该解决方案还能够按原样保留非百分比数字,并以类似的方式显示它们(例如,points_game仅适用于player_a).输出看起来像这样(请原谅MS Paint):

I want to display the fg_perc values as percentages for player_a data only. I can't figure out how to do this without eliminating the legend (when I spread() league_avg). I'm looking for a solution that will also be able to keep non-percentage numbers as-is, and display them in similar fashion (e.g. points_game for player_a only). The output would look something like this (please excuse the MS Paint):

谢谢!

推荐答案

如果要使用 make_chart 函数而不进行硬编码,则需要使用非标准评估(nse).许多 tidyverse 函数具有nse版本,在函数名称后用 _ 表示.如果您以编程方式使用它们,则可以使用这些版本.

If you want to use your make_chart function without hardcoding, you'll want to use non-standard evaluation (nse). Many of the tidyverse functions have nse versions, indicated by a _ after the function name. You would use these versions if you're using them programmatically.

下面将是使用nse版本的 aes filter aes _ filter _ 的粗略示例.使用nse构建功能比我在这里所做的要多得多.建议您阅读 nse上的dplyr小插图更多.

Below would be a rough example of using the nse versions of aes and filter, aes_ and filter_. There is a lot more to building functions using nse than what I've done here. I suggest you read the dplyr vignette on nse for more.

我只是使用 filter _ 而不是 ifelse 复制了@OmaymaS提供的解决方案.我还随意在函数中添加数据参数 .data 以及 player_highlight 参数. .data 使您的函数易于使用,而 player_highight 使您可以选择要突出显示的播放器(因为它们可能不止一个).您很可能想扩展功能,应该这样做!

I simply replicated the solution @OmaymaS provided, with a filter_ rather than the ifelse. I also took the liberty of adding a data parameter .data to your function, along with a player_highlight parameter. The .data lets your function be pipeable and the player_highight lets you choose which player you want to highlight (as they'll probably be more than one). You'll most likely want to expand the functionality, and should do so!

library(tidyverse)

df <- data_frame(
  season = c(2010, 2011, 2012, 2013, 2014,
             2010, 2011, 2012, 2013, 2014),
  player = c("player_a", "player_a", "player_a", "player_a", "player_a",
             "league_avg", "league_avg", "league_avg", "league_avg", "league_avg"),
  fg_perc = c(.4912, .6083, .3095, .5525, .4289,
              .4825, .4836, .4819, .4860, .4848),
  points_game = c(20, 18, 15, 19, 18,
                  12, 12, 13, 11, 12)
)


make_chart <- function(.data, target_column, player_highlight) {
    ggplot(.data, aes_(x = ~season, y = as.name(target_column))) + 
    geom_line(aes_(color = ~player), size = 1.33) +
    geom_text(data = filter_(.data, ~ player == player_highlight), aes_(label = as.name(target_column)))
}

make_chart(df,"fg_perc","player_a")

df %>% 
  make_chart("fg_perc","player_a")

这篇关于ggplot2:仅显示一组中的文本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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