Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

Chart in R not displaying data

I have the following data frame:

        vector_builtin  vector_loop     vector_recursive
1            0.00        0.10             0.34
2            0.00        0.10             0.36
3            0.00        0.08             0.36
4            0.00        0.11             0.34
5            0.00        0.11             0.36

I want to display the three columns in a line chart.

I have imported ggplot2 into R and the chart is displaying without data or lines in it.

Code:

library(ggplot2)
indexes <- row.names(df.new)
ggplot(df.new, aes(x=vector_recursive, y=indexes))

Chart output enter image description here

Output I want A chart showing the three series in a line chart.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make sure that you have your data in a format that ggplot2 understands. In your code, geom_line is expecting you to provide which columns in your data should correspond to which question. Below I have recreated your data, in the future, consider using dput to provide the data to others, which will help troubleshoot your specific issue.

df=data.frame(vector_builtin=c(0.00,0.00,0.00,0.00,0.00),
           vector_loop=c(0.10,0.10,0.08,0.11,0.11),
           vector_recursive=c(0.34,0.36,0.36,0.34,0.36))

However, you don't specify what your x axis is, so we will create a new variable that holds that information, for example:

df$x=1:5

Now, I would recommend reshaping the data into long format, which is preferred by ggplot2. You could also use the other answer here and specify each without that problem, but reshape2's melt function could be used.

library(reshape2)
df.m = melt(df, id.vars="x")

Now when you correctly identify the names of the columns to plot, ggplot2 will plot the data correctly:

ggplot() + geom_line(aes(color=variable, x=x, y=value), data=df.m)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...