Programming Articles - Page 1770 of 3363

How to change the border color of points in a scatterplot created with ggplot2 in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 11:34:02

3K+ Views

Aesthetics is an essential part of a plot whether it is a scatterplot or any other plot. When we create a scatterplot with ggplot function of ggplot2 package, the border of the points is black if we fill the points with the sequence of a color but we can change these borders to any other color by using colour argument.ExampleConsider the below data frameRate

How to create a horizontal bar graph using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 11:30:47

536 Views

Making comparisons is bit easier through horizontal bar graphs as compared to the vertical bar graphs in cases where the labels for the categories have large names. Because a large name for the labels of a vertical bar graph is likely to mix with the other labels and therefore, the reading of these labels become difficult for the viewer. To solve this problem, we can draw a bar graph and flip it with coord_flip() in ggplot2.ExampleConsider the below data frame −Size

How to remove the border of the legend of a chart created by plot function in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 11:28:32

3K+ Views

When we create a chart using plot function and add a legend to that chart, the output of the chart has the legend that is covered with borders. But this breaks the flow of the chart and the area covered by the order make the chart unattractive. Therefore, we can use bty="n" with the legend function and it will remove the border of the legend.ExampleCreating the chart with legend that has border −plot(x=1:10, y=10:1) legend(1, 2, "This is a scatterplot between x and y")OutputCreating the chart with legend which does not have a border −Exampleplot(x=1:10, y=10:1) legend(1, 2, "This is ... Read More

How to initialize a data frame with variable names in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 11:23:35

5K+ Views

There are many ways to initialize a data frame in R but initializing with matrix is the best among them because creating the data frame with matrix help us to avoid entering the wrong number of columns and the wrong number of rows. After initializing the matrix, we can simply use as.data.frame to convert the matrix into a data frame and that’s it.Examples Live Demodf1

How to create colored Venn Diagrams?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 11:16:13

448 Views

Reading transparent Venn Diagrams is a little difficult due to transparency in all areas, therefore, we should create Venn Diagrams that has different color in each of the area. This will help us to easily read the diagram and also the diagram will be more appealing to viewers. We can do this by using venn.diagram function of VennDiagram package.Installing and Loading VennDiagram package −Exampleinstall.packages(“VennDiagram”) library(VennDiagram)Creating the colored Venn Diagram (Note: The colored diagram will be saved in default folder for R in your system, mostly it is documents folder) −venn.diagram(list(x=1:10, y=7:12), fill=c("yellow", "black"), + filename = "Example1.tiff")Outputvenn.diagram(list(x=1:10, y=7:12, z=9:16), fill=c("yellow", ... Read More

How to change ordinal X-axis label to text labels using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 11:01:17

2K+ Views

A plot created with ordinal values on X-axis needs to be ordered for plotting, otherwise, the plot will have continuous values on the X-axis that includes ordinal values. If we want to convert those values to text then scale_x_discrete should be used with the number of breaks, these number of breaks are the actual number of labels we want to use in our plot.ExampleConsider the below data frame −x

How to check if two data frames same or not in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 10:46:51

923 Views

Two data frames can be same if the column names, row names and all the values in the data frame are exactly same. We might to check this for data frames that we expect to be same, for example, if we have two data sets each one of have same number of rows, same number of columns, same data type for each of the columns, and the data view shows that values are same then it is worth checking whether the complete data sets are same or not. To do this checking in R, we can use identical function.Examples Live Demodf1Read More

How to create a line chart using ggplot2 with a vertical line in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 10:40:52

317 Views

In general, the line chart is drawn to view the trend of something and we might also have some threshold point for that trend, for example, if blood pressure is plotted then we might want to show 60 mm Hg as well because this is the lowest acceptable value for blood pressure recommended by doctors. Therefore, it can be plotted as a vertical line if we want to plot blood pressures of a person. Similarly, there can be many situations where we can use a vertical line to visualize the threshold value. This can be achieved in ggplot2 with the ... Read More

How to find the mean of columns of an R data frame or a matrix?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 10:38:38

2K+ Views

If all the columns in an R data frame are numeric then it makes sense to find the mean for each of the columns. This calculation will help us to view how different the values of means are for each of the columns but to make sure that they are significantly different, we will need to run a hypothesis test. To find the column means of a data frame or a matrix we can use colMeans function.ExampleConsider the below data frame − Live Demoset.seed(9) x1

How to extract the p-value and F-statistic from aov output in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 10:35:11

5K+ Views

The analysis of variance technique helps us to identify whether there exists a significant mean difference in more than two variables or not. To detect this difference, we either use F-statistic value or p-value. If the F-statistic value is greater than the critical value of F or if p-value is less than the level of significance then we say that at least one of the means is significantly different from the rest. To extract the p-value and F-statistic value, we can make use of summary function of the ANOVA model.Example Live Demoset.seed(123) Group

Advertisements