The R language Visualization---introduces some powerful drawing and data processing packages and functions (Ggplot,melt ())

Source: Internet
Author: User
Tags vars ggplot

In the recent internship need R language analysis, for the final result needs to be shown below the diagram to show (sample image in the paper interception)


Because of contact with R language soon, just start is a variety of Meng, think directly with R is not drawing such pictures, and then asked the classmate said with R language Ggplot2 expansion package, so found some tutorials on the internet to find this kind of picture can be painted, but its input data form and I have the form of data has a very big difference, The figures of the landlord are as follows (just an example, non-original data)


The row and column headings of the landlord's data are the different model used in the two stages of the experiment, and the corresponding values are the experimental results obtained by using the model combination of their corresponding columns. The online tutorials are mostly standard data sets, such as some properties and their corresponding values. This tutorial recommended http://www.thebigdata.cn/JiShuBoKe/14326.html Click to open the link. Search multiple tutorials did not find a solution for my data, had to do a very foolish thing, my data rolled up and merged into the required data, but also painted the desired results, but very troublesome, and then found a familiar with the R classmate Consulting, someone else a code to solve my a lot of work to expand the merger , is the melt function, the code is as follows


PlotData <-read.csv (file= "~/desktop/a2fscore.csv")
Head (PlotData)
class (PlotData)
Library ( RESHAPE2)
Library (ggplot2)
plotdata <-Melt (plotdata,id.vars = 1)
P <-ggplot (Data=plotdata,aes (x= Variable,y = value,fill=x)) +geom_bar (stat= "Identity", position= ' Dodge ') +ggtitle ("Fscores for A2benchmark") +mycolour _3
P
Focus on the melt () function here, the function in the Reshape2 package. What we need here is a data frame, which is found in the Help document of R melt

Melt A data frame into the form suitable for easy casting melts (splits, decomposes) into a form suitable for conversion (that's a bunch of stupid work I've done before)

It is still quite abstract to say that the help document is helpful in helping the language. According to the information I find on the Internet, it can be simply understood that the corresponding values are linearized according to the names of each dimension of the data to be melt, such as a 2*2 matrix, after melt effect is as follows


a little more complicated for Dataframe's melt ()

Where the Id.vars parameter in the original dataframe in the variable will be used as an identifier, each identifier variable in one column, and the corresponding parameter is Measured.vars, not specified in the case, All variables except those specified as Id.vars are observed, and the names and values of the observed variables occupy one column, representing the variable and value variables in the result.

The melt data can be called Ggplot for drawing, where the function separating the different categories is position = ' dodge '

Overall still feel the use of the data frame melt a bit around, and then think about my data can be directly using As.matrix () to transform the data into a matrix, and then the matrix melt, so better understanding. And I here according to Dataframe processing is actually because I write data when a little problem, the data is written to CSV, added column name, resulting in the reading of data, column name also become part of the measured, self-defeating, but with data frame processing more convenient.

Almost finishing here, write down is not easy to forget, hope also for everyone a little help ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.