A general topology answer: suppose $f: X \rightarrow Y$ has a compact graph $\Gamma(f) = \left\{(x,f(x)): x \in X\right\} \subset X \times Y$. Also suppose $X$ is Hausdorff.
Now let $F \subset Y$ be closed. To show continuity of $f$, it suffices to show that $f^{-1}[F]$ is closed in $X$. Define $\pi: X \times Y \rightarrow X$ by $\pi(x,y) = x$, the projection onto the first coordinate, which is continuous. Then:
$$f^{-1}[F] = \pi[(X \times F) \cap \Gamma(f)]$$
($x \in f^{-1}[F]$ iff $f(x) \in F$, which means $(x,f(x)) \in \Gamma(f) \cap (X \times F)$, and $\pi(x,f(x)) = x$, so $x$ is in the right hand side. On the other hand, if $x$ is in the right hand side, we have $(x,y) \in (X \times F) \cap \Gamma(f)$ for some $y (\in F)$ and being in $\Gamma(f)$ forces $y = f(x)$ , and so $y = f(x) \in F$, so $x \in f^{-1}[F]$.)
Now $X \times F$ is closed in $X \times Y$ so its intersection with $\Gamma(f)$ is closed in $\Gamma(f)$, which is compact. So $(X \times F) \cap \Gamma(f)$ is compact as well, being closed in a compact set. And so its continuous image under $\pi$, which equals $f^{-1}[F]$ as we saw, is compact too. But if $X$ is Hausdorff, this implies it's closed, and we are done.
So the only thing we need of being a subset of $\mathbb{R}$ is that it guarantees Hausdorffness.
Best Answer
If $f$ is continuous, then also the map $g:X\rightarrow\mathbb{R}^2, x\mapsto (x,f(x))$ is continuous. Therefore $g(X)=\{(x,f(x))\,:\,x\in X\}$ is compact.
On the other hand, let $g(X)$ be compact and $x_n\rightarrow x$ be a convergent sequence. We show that $f(x_n)$ converges to $f(x)$. Since the graph is compact, $f(x_n)$ has a convergent subsequence, i.e. $f(x_{n_j})\rightarrow y$. That is $(x_{n_j}, f(x_{n_j}))\rightarrow (x, y)$. The graph is closed. That is, the limit of every convergent sequence in $g(X)$ is again in $g(X)$. Therefore $(x,y)\in g(X)$, i.e. $y=f(x)$.
Since this is true for every convergent subsequence, we showed that $f(x)$ is the only accumulation point of $f(x_n)$, i.e. $f(x_n)$ converges to $f(x)$.
This is also known as the closed graph theorem.