Question

In: Computer Science

For the following plots, rewrite the equivalent code in ggplot2 grammar. qplot(displ, hwy, data = mpg,...

  1. For the following plots, rewrite the equivalent code in ggplot2 grammar.

qplot(displ, hwy, data = mpg, geom = c("point", "smooth"))

qplot(color, data = diamonds, fill = cut, position = "identity")

Solutions

Expert Solution

The solution for the above problem is given below and if you feel any problem then feel free to ask:

qplot(displ, hwy, data = mpg, geom = c("point", "smooth"))

Here, the displ is for the x-axis and hwy is for the y-axis which are the aesthetics for the layer, mpg is the data frame to use and geom is setting the behavior of the plot to draw as smooth points.

So, in ggplot2 the qplot is converted as below:

ggplot(mpg, aes(displ, hwy)) + geom_point() + geom_smooth()

Here, the data frame is given first and aesthetics are given in aes() function also the geoms are given separately as point and smooth.

Now for the second plot statement:

qplot(color, data = diamonds, fill = cut, position = "identity")

Here, the only x is supplied to qplot as color therefore the resulting in formation of bars or histogram, diamonds is the data frame to use, fill is used to control the color areas of histogram which is set to cut to fill the histogram bars based on the cut and the position is used for overlapping or proper formatting of colors which is given identity but this position gets deprecated in case of qplot.

So, in ggplot2 the qplot statement is converted as given below:

ggplot(diamonds, aes(color, fill = cut)) + geom_bar(position = "stack")

Here, the aesthetics are passed as color which is x for the graph and fill = cut and in addition the geom_bar() function is passed with position="stack" as in ggplot you have to mention the shape of the plot and in case of position ="identity" all the colors get overlapped by the one color which is not going to happen in qplot so you have to use position="stack" so that all colors can appear as required but if you want to use position="identity" then you can simply replace the stack by identity but for same functioning as qplot use stack in position.


Related Solutions

Car Size Displacement Cylinders Drive Fuel Type City MPG Hwy MPG a.     Use the descriptive statistics...
Car Size Displacement Cylinders Drive Fuel Type City MPG Hwy MPG a.     Use the descriptive statistics tool to give the descriptive stats for the quantitative variables. 1 Compact 2.0 4 F P 22 30 b.     What is the highest City MPG? HWY MPG? The lowest? What is the mean, median, mode for each? 2 Compact 2.0 4 A P 21 29 c.      Use the PivotChart tool to develop a histogram of the data. 3 Compact 2.0 4 A P 21...
Part A: Correctly (& concisely) rewrite each of the following sentences. Check for spelling, grammar, subject-to-verb...
Part A: Correctly (& concisely) rewrite each of the following sentences. Check for spelling, grammar, subject-to-verb match, punctuation, format and structure errors. Less is best.                                                                                                                                                                                                                                          1. Some techniques for sending a email would be to have the paragraphs aligned, bullets, and direct points when communicating. 2. As you may know Amazon is retail store who is worth over 250 billion dollars in market value, now with Covid 9 and everyone online, their market value is expected to increase dramatically....
NEED TO REWRITE THIS IN OOP MODE ######## HOMEWORK 19 ###### Rewrite the code in the...
NEED TO REWRITE THIS IN OOP MODE ######## HOMEWORK 19 ###### Rewrite the code in the OOP mode (class mode). ########################### lambda trick ##### first: changing label properties: after you've created a label, you ##### may want to change something about it. To do that, use configure method: ## label.configure(text = 'Yes') ## label.configure(bg = 'white', fg = 'black') ### that change the properties of a label called label. ################################################### ## from tkinter import * abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def callback(x):...
Problem set Rewrite the following if statement as an equivalent switch statement. The variable digit is...
Problem set Rewrite the following if statement as an equivalent switch statement. The variable digit is of type int. if (digit == 0) value = 3; else if (digit == 1) value = 3; else if (digit == 2) value = 6; else if (digit == 3)     value = 9; The decision table below shows fines imposed for speeding violations. Write a code segment that assigns the correct fine to type double variable fine based on the value of...
Review all rules in Appendix A about punctuation, number usage, and grammar. Then, rewrite each sentence...
Review all rules in Appendix A about punctuation, number usage, and grammar. Then, rewrite each sentence to make all needed corrections. A. If you go to just about any company’s website you’ll find many references to corporate social responsibility, or CSR for short. B. In fact, many companies have dedicate entire sections on they’re websites to CSR. They even publish CSR reports under names such as Giving Back, Sharing the Wealth, and Citizenship Report. C. The reality, however, is corporate...
Rewrite this code of a game of Moropinzee so that it works as intended without the...
Rewrite this code of a game of Moropinzee so that it works as intended without the "break;" in the last few lines of the code. Code: import java.util.*; public class Moropinzee { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(true) { System.out.println("Player 1 enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:"); int p1 = sc.nextInt(); while(p1<1 || p1>5) { System.out.println("Invalid choice, Player 1. Enter a number 1-5:"); p1 = sc.nextInt(); } System.out.println("Player 2...
Given the following data where city MPG is the response variable and weight is the explanatory...
Given the following data where city MPG is the response variable and weight is the explanatory variable, explain why a regression line would be appropriate to analyze the relationship between these variables: Model City MPG Weight Mazda MX-5 Miata 25 2365 Mercedes/Benz SLK 22 3020 Mitsubishi Eclipse 23 3235 Pontiac Firebird 18 3545 Porsche Boxster 19 2905 Saturn SC 27 2420 Construct the regression line for this data. Interpret the meaning of the y-intercept and the slope within this scenario....
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void...
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void loop() { // Print the value inside of myBPM. Serial.begin(9600); int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int". // "myBPM" hold this BPM value now. if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened". Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened". Serial.print("BPM:...
Use R to complete the following questions. You should include your R code, output and plots...
Use R to complete the following questions. You should include your R code, output and plots in your answer. 1. Two methods of generating a standard normal random variable are: a. Take the sum of 5 uniform (0,1) random numbers and scale to have mean 0 and standard deviation 1. (Use the properties of the uniform distribution to determine the required transformation). b. Generate a standard uniform and then apply inverse cdf function to obtain a normal random variate (Hint:...
Use R to complete the following questions. You should include your R code, output and plots...
Use R to complete the following questions. You should include your R code, output and plots in your answer. 1. Two methods of generating a standard normal random variable are: a. Take the sum of 5 uniform (0,1) random numbers and scale to have mean 0 and standard deviation 1. (Use the properties of the uniform distribution to determine the required transformation). b. Generate a standard uniform and then apply inverse cdf function to obtain a normal random variate (Hint:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT