Programming Articles - Page 893 of 3363

Remove grey color from legend display using ggplot2 in R.

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:04:23

4K+ Views

To remove grey color from legend display using ggplot2, we can use theme function where we can fill legend.key argument to white with element_rect.For Example, if we have a data frame called df that contains three columns say X and Y and F where X and Y are numerical and F is categorical then we can create scatterplot between X and Y without grey color in legend display by using the command given below −ggplot(df, aes(X, Y))+geom_point(aes(colour=factor(F)))+theme(legend.key=element_rect(fill="white"))ExampleFollowing snippet creates a sample data frame −xRead More

How to cover legend in a box using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 05:58:37

2K+ Views

To cover legend in a box using ggplot2 in R, we can use theme function with legend.box.background and legend.box.margin argument. The legend.box.background will have a rectangular element with the help of element_rect and margin values will be set in legend.box.margin.Check out the Example given below to understand how it can be done.ExampleFollowing snippet creates a sample data frame −Score

How to find the square root of each value in columns if some columns are categorical in R data frame?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:01:24

237 Views

To find the square root of each value in columns if some columns are categorical in R data frame, we can follow the below steps −First of all, create a data frame.Then, use numcolwise function from plyr package to find the square root of each value in columns if some columns are categorical.ExampleCreate the data frameLet’s create a data frame as shown below −Level

How to display legend on top using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 05:49:36

180 Views

To display legend on top using ggplot2 in R, we can use theme function with legend.justification argument to top.For Example, if we have a data frame called df that contains three columns say X and Y and F where X and Y are numerical and F is categorical then we can create scatterplot between X and Y with point colored by values in F by using the command given below −ggplot(df,aes(X,Y))+geom_point(aes(colour=factor(F)))+theme(legend.justification="top")ExampleFollowing snippet creates a sample data frame −x

How to change the text size of Y-axis title using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 05:45:18

4K+ Views

By default, the text size of axes titles are small but if we want to increase that size so that people can easily recognize them then theme function can be used where we can use axis.title.y argument for Y-axis and axis.title.x argument for X-axis with element_text size to larger value.Check out the Example given below to understand how it can be done.ExampleFollowing snippet creates a sample data frame −Country

How to find the column minimum if some columns are categorical in R data frame?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 05:56:37

163 Views

To find the column minimum if some columns are categorical in R data frame, we can follow the below steps −First of all, create a data frame.Then, use numcolwise function from plyr package to find the column minimum if some columns are categorical.ExampleCreate the data frameLet’s create a data frame as shown below −Group

How to increase the axes tick width using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 05:39:50

5K+ Views

To increase the width of axes tick (both X-axis and Y-axis at the same time) using ggplot2 in R, we can use theme function with axis.ticks argument where we can set element_line argument size to a larger value.For Example, if we have a data frame called df that contains a single column X and we want to create histogram of X with wider axes ticks then we can use the command given below −ggplot(df,aes(X))+geom_histogram(bins=30)+theme(axis.ticks=element_line(size=2))ExampleFollowing snippet creates a sample data frame −x

How to find the row median of columns having same name in R data frame?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 05:41:24

287 Views

To find the row median of columns having same name in R data frame, we can follow the below steps −First of all, create a data frame with some columns having same name.Then, use tapply along with colnames and median function to find the row median of columns having same name.ExampleCreate the data frameLet’s create a data frame as shown below −df

How to increase the width of axes using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 05:35:27

7K+ Views

To increase the width of axes (both X-axis and Y-axis at the same time) using ggplot2 in R, we can use theme function with axis.line argument where we can set element_line argument to a larger value.Check out the Example given below to understand how it can be done.ExampleFollowing snippet creates a sample data frame −x

How to change the border style of a plot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 05:30:30

2K+ Views

To change the border style of a plot using ggplot2 in R, we can use theme function with panel.border argument where we can change the linetype for the plot border using element_rect. There are many linetypes and we can use them as desired.Check out the Example given below to understand how it can be done.ExampleFollowing snippet creates a sample data frame −x

Advertisements