in a single ggplot2 plot plot three vectors of data of different legnht in differenct colors in r

To plot three vectors of data of different lengths in different colors using ggplot2 package in R, you can create a new data frame with the three vectors as columns, each with a corresponding label column. Then, you can use the melt() function from the reshape2 package to convert the data frame from wide to long format, with a separate row for each combination of labels and values.

Once the data frame is in the desired format, you can use ggplot() function to create a scatter plot with the values as the y-axis variable, labelled by the label column, and colored by the variable column.

Here is an example code snippet:

main.r
library(ggplot2)
library(reshape2)

# Create example data
vec1 <- c(1, 3, 5)
vec2 <- c(2, 4, 6, 8)
vec3 <- c(3, 6, 9, 12, 15)
labels <- rep(c("A", "B", "C"), c(length(vec1), length(vec2), length(vec3)))

# Combine vectors into data frame
df <- data.frame(vec1, vec2, vec3, labels)

# Melt data to long format
df_long <- melt(df, id.vars = "labels")

# Plot data using ggplot2
ggplot(df_long, aes(x = labels, y = value, color = variable)) + 
  geom_point(size = 3) + 
  labs(x = "Labels", y = "Values", color = "Variables")
523 chars
20 lines

This will create a scatter plot with the values of each vector plotted in different colors, labeled by the corresponding label column.

gistlibby LogSnag