سؤال

I have a dataframe as such:

1   Pos like    77
2   Neg like    58
3   Pos make    44
4   Neg make    34
5   Pos movi    154
6   Neg movi    145    
...
20  Neg will    45

I would like to produce a plot using the geom_text layer in ggplot2.

I have used this code

q <- ggplot(my_data_set, aes(x=value, y=value, label=variable)) 
q <- q + geom_text()
q

which produced this plot:

enter image description here

Obviously, this is not an ideal plot.

I would like to produce a plot similar, except I would like to have the Positive class on the x-axis, and the Negative class on the y-axis.

UPDATE: Here is an example of something I am attempting to emulate:

enter image description here

I can't seem to figure out the correct way to give the arguments to the geom_line layer.

What is the correct way to plot the value of the Positive arguments on the X-axis, and the value of the Negative arguments on the Y-axis, given the data frame I have?

Thanks for your attention.

هل كانت مفيدة؟

المحلول

my_data_set <- read.table(text = "
id variable value
Pos like    77
Neg like    58
Pos make    44
Neg make    34
Pos movi    154
Neg movi    145", header = T)

library(data.table)
my_data_set <- as.data.frame(data.table(my_data_set)[, list(
                      Y = value[id == "Neg"],
                      X = value[id == "Pos"]),
               by = variable])

library(ggplot2)
q <- ggplot(my_data_set, aes(x=X, y=Y, label=variable)) 
q <- q + geom_text()
q

enter image description here

نصائح أخرى

This can also be easily done with reshape2 (with the same result as David Arenburg's answer):

df <- read.table(text = "id variable value
Pos like    77
Neg like    58
Pos make    44
Neg make    34
Pos movi    154
Neg movi    145", header = TRUE)

require(reshape2)
df2 <- dcast(df, variable ~ id, value.var="value")

library(ggplot2)
ggplot(df2, aes(x=Pos, y=Neg, label=variable)) + 
  geom_text()

which results in: enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top