可以在带状图中标记数据点吗? [英] Can data points be labeled in stripcharts?

查看:104
本文介绍了可以在带状图中标记数据点吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到在stripchart中标记数据点的方法.如问题中所建议的那样,使用text函数会在点堆叠或分解时崩溃.紧张不安.

I can't find a way to label datapoints in stripchart. Using the text function, as suggested in this question, breaks down when points are stacked or jittered.

我有4类(第2-5列)的数值数据,并想用首字母缩写(第1列)标记每个数据点.

I have numerical data in 4 categories (columns 2-5) and would like to label each datapoint with the initials (column 1).

这是我的数据和我尝试过的代码:

This is my data and the code I have tried:

initials,total,interest,slides,presentation
CU,1.6,1.7,1.5,1.6
DS,1.6,1.7,1.5,1.7
VA,1.7,1.5,1.5,2.1
MB,2.3,2.0,2.1,2.9
HS,1.2,1.3,1.4,1.0
LS,1.8,1.8,1.5,2.0

stripchart(CTscores[-1], method = "stack", las = 1)
text(CTscores$total + 0.05, 1, labels = CTscores$name, cex = 0.5)

以下情节是迄今为止我管理的最好的情节.如您所见,数据点标签重叠.另外,最长的y标签被剪掉.

The plot below is the best I managed so far. As you see, the data point labels overlap. In addition, the longest y label is cut off.

可以在带状图中标记点吗?还是我必须将其与另一个命令一起显示才能进行标记?

Can points be labelled in a strip chart? Or do I have to display this with another command to allow for labeling?

推荐答案

以下是一种替代方法,允许您向带状图添加颜色以识别缩写:

Here's an alternative that allows you to add color to a strip chart in order to identify the initials:

library(ggplot2)
library(reshape2)
library(gtable)
library(gridExtra)

# Gets default ggplot colors
gg_color_hue <- function(n) {
  hues = seq(15, 375, length=n+1)
  hcl(h=hues, l=65, c=100)[1:n]}

# Transform to long format
CTscores.m = melt(CTscores, id.var="initials")

# Create a vector of colors with keys for the initials
colvals <- gg_color_hue(nrow(CTscores))
names(colvals) <- sort(CTscores$initials)

# This color vector needs to be the same length as the melted dataset
cols <- rep(colvals,ncol(CTscores)-1)

# Create a basic plot that will have a legend with the desired attributes
g1 <- ggplot(CTscores.m, aes(x=variable, y=value, fill=initials)) +
  geom_dotplot(color=NA)+theme_bw()+coord_flip()+scale_fill_manual(values=colvals)

# Extract the legend
fill.legend <- gtable_filter(ggplot_gtable(ggplot_build(g1)), "guide-box") 
legGrob <- grobTree(fill.legend)

# Create the plot we want without the legend
g2 <- ggplot(CTscores.m, aes(x=variable, y=value)) +
  geom_dotplot(binaxis="y", stackdir="up",binwidth=0.03,fill=cols,color=NA) +
  theme_bw()+coord_flip()

# Create the plot with the legend
grid.arrange(g2, legGrob, ncol=2, widths=c(10, 1))

这篇关于可以在带状图中标记数据点吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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