How to define a function and produce its line chart?

hi guys, I'm trying to finish a little exercise which needs to do the temperature conversion and drawing line graph, the problem is that i need a line chart, but there isn't this type of graph in the function 'curve', so is there any other function I can use? Thank you

Here is the line chart I need to produce.

Here is my code

CtoF <- function(C){
     F <- 9/5*C + 32
     return(F)
 }
 curve(CtoF, from = 0, to = 50, type = 'l')

and here is the graph I produce.

CtoF <- function(C){
  F <- 9/5*C + 32
  return(F)
}

par(pty="s")
curve(CtoF, from = 0, to = 50, type = 'l')

thank you but here is another problem, could you please tell me how to add plots in this line chart?

If you mean both points and lines \dots

CtoF <- function(C){
  F <- 9/5*C + 32
  return(F)
}

par(pty="s")
curve(CtoF, from = 0, to = 50, type = 'b')

which would look better with fewer points

CtoF <- function(C){
  F <- 9/5*C + 32
  return(F)
}

par(pty="s")
curve(CtoF, from = 0, to = 50, n = 10, type = 'b')

Created on 2021-03-07 by the reprex package (v1.0.0)

that's exactly what I need, thank you a lot.
Could you please help me with another similar question?
Here is the graph I need.


Here is the graph I produce.

Here is my code.

fit <- lm(Mort~Lat, data = skin)
plot(skin$Lat, skin$Mort)
abline(fit, col=2)

I have tried 'par(pty="s")', but it changes nothing.

Thank you!

See the FAQ: How to do a minimal reproducible example reprex for beginners. Without the skin dataframe I can't be confident in a specific answer.

However, abiline needs more to do its job

abline(a = NULL, b = NULL, h = NULL, v = NULL, reg = NULL,
coef = NULL, untf = FALSE, ...)

where

a, b are the intercept and slope, single values

and those haven't been provided.

So \dots the next question is how to find \dots

I think it is more likely to be the problem of the 'plot' function, because the graph is rectangle with and without the 'abline' function.

I have tried to modify the parameters of 'plot' function, but it is still wrong. There is data skin in the link, which is a really really small file. Could you please have a look?

https://drive.google.com/file/d/1ABwJ-B6kdBv4lZWi7uQYOoLuy60iVZF4/view?usp=sharing

Here is my code.

skin <- read.table('skincancer.txt', header = T)
summary(skin)
head(skin)
fit <- lm(Mort~Lat, data = skin)
plot(skin$Lat, skin$Mort, 
     xlab = 'Degré de latitude nord', 
     ylab = "Nombre de décès pour 10 milions d'habitudes")
abline(fit, col=2)
summary(fit)

Sorry, still can't reproduce the problem

skin <- data.frame(
  State =
    c("Alabama", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "D.C.", "Florida", "Georgia", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "NewHampshire", "NewJersey", "NewMexico", "MewYork", "NorthCarolina", "NorthDakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "RhodeIsland", "SouthCarolina", "SouthDakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "WestVirginia", "Wisconsin", "Wyoming"),
  Lat =
    c(33, 34.5, 35, 37.5, 39, 41.8, 39, 39, 28, 33, 44.5, 40, 40.2, 42.2, 38.5, 37.8, 31.2, 45.2, 39, 42.2, 43.5, 46, 32.8, 38.5, 47, 41.5, 39, 43.8, 40.2, 35, 43, 35.5, 47.5, 40.2, 35.5, 44, 40.8, 41.8, 33.8, 44.8, 36, 31.5, 39.5, 44, 37.5, 47.5, 38.8, 44.5, 43),
  Mort =
    c(219, 160, 170, 182, 149, 159, 200, 177, 197, 214, 116, 124, 128, 128, 166, 147, 190, 117, 162, 143, 117, 116, 207, 131, 109, 122, 191, 129, 159, 141, 152, 199, 115, 131, 182, 136, 132, 137, 178, 86, 186, 229, 142, 153, 166, 117, 136, 110, 134),
  Ocean =
    c(1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0),
  Long =
    c(87, 112, 92.5, 119.5, 105.5, 72.8, 75.5, 77, 82, 83.5, 114, 89.5, 86.2, 93.8, 98.5, 85, 91.8, 69, 76.5, 71.8, 84.5, 94.5, 90, 92, 110.5, 99.5, 117, 71.5, 74.5, 106, 75.5, 79.5, 100.5, 82.8, 97.2, 120.5, 77.8, 71.5, 81, 100, 86.2, 98, 111.5, 72.5, 78.5, 121, 80.8, 90.2, 107.5)
)

fit <- lm(Mort ~ Lat, data = skin)
par(pty="s")
plot(skin$Lat, skin$Mort,
  xlab = "Degré de latitude nord",
  ylab = "Nombre de décès pour 10 milions d'habitudes")

abline(fit, col = 2)

I think its a question of the plot window size,which is adjustable in the IDE,

it is weird​:face_with_monocle::face_with_monocle::face_with_monocle:, I still can't get a same graph​:joy:

maybe, well I will try to change the settings, thanks

The tidyverse solution, just change the aspect ratio to whatever you need. A 1 would produce a perfect square. In RStudio the shape of the graph is determined by the shape of the plot window unless you take control. At least when I copy a graph, then change from "full screen" to not full screen the copied graph changes aspect ratio because when I then paste into PowerPoint the graphs do not line up.

if(!require(tidyverse)){install.packages("tidyverse")}
library(tidyverse)
nord<-seq(30,50,by=0.033)
set.seed(24556)
nombre<-200-nord+rnorm(607,10,7)
Mydf <- data.frame(nord,nombre)
ggplot(Mydf, aes(x=nord, y=nombre)) + geom_point() + geom_smooth() + 
  theme(aspect.ratio = 3.5)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.