मुझे stripchart
में डेटा पॉइंट्स लेबल करने का कोई तरीका नहीं मिल रहा है। इस {href="https://stackoverflow.com/questions/8104733/adding-labels-to-stripchart"> प्रश्न में सुझाए अनुसार text
} फ़ंक्शन का उपयोग करते हुए, अंक होने पर टूट जाता है ढेर या घबराना।
मेरे पास 4 श्रेणियों (स्तंभ 2-5) में संख्यात्मक डेटा है और प्रत्येक डेटापॉइंट को आद्याक्षर (कॉलम 1) के साथ लेबल करना चाहते हैं।
यह मेरा डेटा और कोड मैंने कोशिश की है:
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 लेबल को काट दिया जाता है।
क्या पॉइंट्स को स्ट्रिप चार्ट में लेबल किया जा सकता है? या क्या मुझे लेबलिंग की अनुमति देने के लिए किसी अन्य कमांड के साथ इसे प्रदर्शित करना होगा?
2 जवाब
यहां एक विकल्प है जो आपको इनीशियल्स की पहचान करने के लिए एक स्ट्रिप चार्ट में रंग जोड़ने की अनुमति देता है:
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))
अलग-अलग लेबल रखने के बजाय बिंदु मार्कर के रूप में लेबल का उपयोग करने के बारे में क्या? आधार ग्राफिक्स के बजाय ggplot2
का उपयोग करने का एक उदाहरण है।
ओवरलैप से बचने के लिए, हम सीधे यादृच्छिक मूल्यों के लिए ऊर्ध्वाधर ऑफसेट की मात्रा निर्धारित करते हैं, बजाय इसे यादृच्छिक घबराहट के। ऐसा करने के लिए, हमें संख्यात्मक y-मान निर्दिष्ट करने की आवश्यकता है (ताकि हम ऑफसेट जोड़ सकें) और फिर संख्यात्मक पाठ लेबल को उपयुक्त पाठ लेबल के साथ बदल दें।
library(ggplot2)
library(reshape2)
library(dplyr)
# Convert data from "wide" to "long" format
CTscores.m = melt(CTscores, id.var="initials")
# Create an offset that we'll use for vertically separating the repeated values
CTscores.m = CTscores.m %>% group_by(variable, value) %>%
mutate(repeats = ifelse(n()>1, 1,0),
offset = ifelse(repeats==0, 0, seq(-n()/25, n()/25, length.out=n())))
ggplot(CTscores.m, aes(label=initials, x=value, y=as.numeric(variable) + offset,
color=initials)) +
geom_text() +
scale_y_continuous(labels=sort(unique(CTscores.m$variable))) +
theme_bw(base_size=15) +
labs(y="", x="") +
guides(color=FALSE)
पूर्णता के लिए, यहाँ एक विशिष्ट ऑफसेट के बजाय दोहराया मूल्यों के लिए घबराना के साथ ग्राफ कैसे बनाया जाए:
# Convert data from "wide" to "long" format
CTscores.m = melt(CTscores, id.var="initials")
# Mark repeated values (so we can selectively jitter them later)
CTscores.m = CTscores.m %>% group_by(variable, value) %>%
mutate(repeats = ifelse(n()>1, 1,0))
# Jitter only the points with repeated values
set.seed(13)
ggplot() +
geom_text(data=CTscores.m[CTscores.m$repeats==1,],
aes(label=initials, x=value, y=variable, color=initials),
position=position_jitter(height=0.25, width=0)) +
geom_text(data=CTscores.m[CTscores.m$repeats==0,],
aes(label=initials, x=value, y=variable, color=initials)) +
theme_bw(base_size=15) +
guides(color=FALSE)