हे,
मैं एक्स-अक्ष या (2) ग्राफ के स्टैक बार (या कुछ समान) के शीर्ष पर एकीकृत (1) की गहराई सीमा की जानकारी जोड़ने की कोशिश कर रहा हूं। नीचे दी गई तस्वीरों को देखें।
लेबल के साथ: छवि विवरण यहां दर्ज करें
(१) सुनिश्चित नहीं है कि यह कैसे करना है।
(२) स्टैक्ड बार के शीर्ष पर इन्फोस प्लॉट करने के लिए मैंने पहले वेरिएबल बनाया है:
depth_int <- c("5-200 m", "5-181 m", "5-200 m", "5-200 m", "5-155 m", "5-200 m", "5-200 m",
"5-90 m", "10-60 m", "10-90 m", "10-30 m", "20-90 m", "0.5-30 m", "0.5-90 m", "0.5-90 m")
और मेरे ggplot() पर इस फ़ंक्शन का उपयोग करके: geom_text(aes(label = depth_int), hjust = 0, position = "stack")
मुझे यह त्रुटि मिलती है: Error: Aesthetics must be either length 1 or the same as the data (164): label
(मुझे इसका अनुमान है क्योंकि स्टैक्ड बार टैक्स का संयोजन है और फिर आप प्रति स्टेशन बार के शीर्ष पर केवल 1 मान प्लॉट नहीं कर सकते हैं (उदाहरण के लिए, पी 1)।
-
यह मेरी स्क्रिप्ट है:
ggplot(df, aes(x=locationID, fill=class, y = V1))+
geom_bar(stat="identity", position = "stack", width = 0.9)+
facet_grid(. ~ expedition, scales="free_x") +
#scale_fill_manual(values = default_colors, labels= c("","","")) #default_colors<-c("#F8766D", "#00BA38", "#619CFF")
# change the label names in the legend
labs(title = "Taxa abundance per depth integrated", fill = "Taxa",
x= bquote('Stations'),
y= bquote('Abundance'~(cells~m^-3)))+
theme_minimal()+
theme(panel.grid.major.y = element_line(size = 0.5, linetype = 'solid',
colour = "grey75"),
panel.grid.minor.y = element_line(size = 0.5, linetype = 'solid',
colour = "grey75"),
panel.grid.major.x = element_blank(),
panel.background = element_rect(fill = "white", colour = "grey75"),
axis.text.x = element_text(colour="black",size=7),
axis.text.y = element_text(colour="black",size=10),
axis.title.x = element_text(colour="black",size=10),
axis.title.y = element_text(colour="black",size=10),
plot.title = element_text(colour = "black", size = 10, face = "bold"),
legend.position = "right",
legend.text = element_text(size=10),
legend.title = element_text(size = 11, hjust =0.5, vjust = 3, face = "bold"),
legend.key.size = unit(10,"point"),
legend.spacing.y = unit(-.25,"cm"),
legend.margin=margin(0,5,8,5),
legend.box.margin=margin(-10,10,-3,10),
legend.box.spacing = unit(0.5,"cm"),
plot.margin = margin(2,2,0,0))
1 उत्तर
केवल सदिश लेबल को geom_text
पर पास करने से काम नहीं चलेगा। अपना वांछित परिणाम प्राप्त करने के लिए आप कर सकते हैं
स्थान और अभियान के आधार पर बार की कुल ऊंचाई प्राप्त करने के लिए अपने डेटा को सारांशित करें
अपने लेबल को सारांशित डेटाफ़्रेम में जोड़ें। इसके लिए मैं स्थान और अभियान द्वारा लेबल के डेटाफ्रेम का उपयोग करता हूं जिसे मैं संक्षेप में df.
सारांशित डेटाफ़्रेम का उपयोग
geom_text
में डेटा के रूप में करें
नोट: जैसा कि संक्षेप में df में कोई कॉलम नहीं है class
मैंने fill=class
को स्थानीय aes
के रूप में geom_bar
के अंदर रखा है।
कुछ यादृच्छिक उदाहरण डेटा का उपयोग करके इसे आजमाएं:
library(ggplot2)
library(tidyr)
library(dplyr)
# Random example dataset
set.seed(42)
df <- data.frame(
locationID = c(sample(LETTERS[1:4], 50, replace = TRUE), sample(LETTERS[5:8], 50, replace = TRUE)),
class = sample(letters[1:4], 100, replace = TRUE),
expedition = rep(paste("expedition", 1:2), each = 50),
V1 = runif(100)
)
# Make a dataframe of labels by expedition and locationID
depth_int <- tidyr::expand(df, nesting(expedition, locationID))
depth_int$label <- c("5-200 m", "5-181 m", "5-200 m", "5-200 m", "5-90 m", "10-60 m", "10-90 m", "10-30 m")
depth_int
#> # A tibble: 8 x 3
#> expedition locationID label
#> <chr> <chr> <chr>
#> 1 expedition 1 A 5-200 m
#> 2 expedition 1 B 5-181 m
#> 3 expedition 1 C 5-200 m
#> 4 expedition 1 D 5-200 m
#> 5 expedition 2 E 5-90 m
#> 6 expedition 2 F 10-60 m
#> 7 expedition 2 G 10-90 m
#> 8 expedition 2 H 10-30 m
# Summarise your data to get the height of the stacked bar and join the labels
df_labels <- df %>%
group_by(locationID, expedition) %>%
summarise(V1 = sum(V1)) %>%
left_join(depth_int, by = c("locationID", "expedition"))
ggplot(df, aes(x=locationID, y = V1))+
geom_bar(aes(fill=class), stat="identity", position = "stack", width = 0.9) +
# Use summarised data to put the labels on top of the stacked bars
geom_text(data = df_labels, aes(label = label), vjust = 0, nudge_y = 1) +
facet_grid(. ~ expedition, scales="free_x") +
labs(title = "Taxa abundance per depth integrated",
fill = "Taxa",
x= bquote('Stations'),
y= bquote('Abundance'~(cells~m^-3)))+
theme_minimal()+
theme(panel.grid.major.y = element_line(size = 0.5, linetype = 'solid',
colour = "grey75"),
panel.grid.minor.y = element_line(size = 0.5, linetype = 'solid',
colour = "grey75"),
panel.grid.major.x = element_blank(),
panel.background = element_rect(fill = "white", colour = "grey75"),
axis.text.x = element_text(colour="black",size=7),
axis.text.y = element_text(colour="black",size=10),
axis.title.x = element_text(colour="black",size=10),
axis.title.y = element_text(colour="black",size=10),
plot.title = element_text(colour = "black", size = 10, face = "bold"),
legend.position = "right",
legend.text = element_text(size=10),
legend.title = element_text(size = 11, hjust =0.5, vjust = 3, face = "bold"),
legend.key.size = unit(10,"point"),
legend.spacing.y = unit(-.25,"cm"),
legend.margin=margin(0,5,8,5),
legend.box.margin=margin(-10,10,-3,10),
legend.box.spacing = unit(0.5,"cm"),
plot.margin = margin(2,2,0,0))
locationID
औरexpedition
के साथ समस्या त्रुटि कहां हुई यह देखने के लिएrlang::last_error()
चलाएं।depth.int
में मौजूद हैं या नहीं उदा।str()
।